VadimDez/ng2-pdf-viewer

PDF blank on first api call, renders when I re-run api call a 2nd time

Eldorados opened this issue · 1 comments

Just started trying to use this lib and having an odd issue

Testing with the most basic setup, I have an api that returns the pdf as a blob

I setup a button to click to call and then render the pdf, the first time clicking the button nothing renders, the call completes successfully and I can see the pdf is returned, but the page is blank

If I click the button a 2nd time (and every click after that), the pdf is rendered as it should

I added the page-rendered event and it's not getting fired the first time either, but does each time after

html:
<pdf-viewer [src]="pdfSrc" [original-size]="false" style="width: 400px; height: 500px; min-height: 200px;"></pdf-viewer>

ts:

  • note I have tried it both ways with either createObjectURL or ArrayBuffer, same result
		this.blob = null;
		this.myService.viewPDF(caseId).subscribe((data) => {
			this.blob = new Blob([data], { type: 'application/pdf' });
			this.pdfSrc = window.URL.createObjectURL(this.blob);

Ive faced a similair issue.

What worked for me was

@ViewChild('pdfViewer') viewer: PdfViewerComponent;


this.pdfSrc = window.URL.createObjectURL(this.blob);

// allow binding to update and force a refresh
setTimeout(() => this.viewer.refresh, 100);