I am using ng2-pdf-viewer
in my Angular 8 application.
I would like to show the PDF fit to the height of the container regardless of the width. So, PDF must take all of the height as we can do by selecting Page Fit
from the zoom option in Mozilla's PDF (link here):
Here is what I have got right now:
My code for ng2-pdf-viewer
is:
<pdf-viewer
[src]='"./assets/images/userFiles/" + file.fileName'
[original-size]="false"
[render-text]='false'
[show-all]="false"
style="display: block; height: 100%;"
(after-load-complete)='afterLoadComplete($event)'
(page-rendered)='pageRendered($event)'
(pageChange)='pageChange($event)'
></pdf-viewer>
I have tried countless things for days to get this working. fit-to-page
, giving height
to all of the containers and much more.
Let me know, what is the best way to get it done
After a lot of efforts, I have found a way for making PDFs stretch to the height of container. I don't know whether it is good approach or not, but it is working great in my scenario.
css
in your main style.scss
or style.css
:.ng2-pdf-viewer-container {
overflow: hidden;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
}
ng2-pdf-viewer
component into your component like this:// other imports
import { PdfViewerComponent } from 'ng2-pdf-viewer';
export class YourComponent{
@ViewChild(PdfViewerComponent, {static: false})
private pdfComponent: PdfViewerComponent;
// other code
}
(page-rendered)
event function to edit the currentScaleValue
of the pdfViewer
. Please check code below:
In html file:
<div style='position: relative; height: 100%;'>
<pdf-viewer [src]='"./assets/images/userFiles/" + file.fileName'
[show-all]="false"
[autoresize]="true"
[original-size]='true'
[fit-to-page]='true'
[render-text]='false'
(page-rendered)="pageRendered()"
></pdf-viewer>
</div>
In your component file:
// called after pdf page is rendered
pageRendered() {
this.pdfComponent.pdfViewer.currentScaleValue = 'page-fit';
}
That's it. Please note, you will notice a flicker or glitch when pdf is updated or browser screen is resized. It is because, ng2-pdf-viewer
changing the height according the it's settings and then when pdf page is rendered, we change it to the page-fit
. I hide the pdf for a while when it updates and then show after it loads the settings to the page-fit
.
Definitely, there are some good approaches to do this. But, this is the only one, I have found after a search of months.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With