I have an application that uses ng2-pdfjs-viewer
to display a pdf and I would like to hide the div containing the viewer when the viewer's source is null. I have looked at a similar post, however this is not the same as what I am trying to accomplish. Here is my code:
component.ts
//onChange
onChange(event){
this.pdfViewerAutoLoad.pdfSrc = "";
}
//convert and display
@ViewChild('pdfViewerAutoLoad', {static:false}) pdfViewerAutoLoad;
pdf: ArrayBuffer;
_base64ToArrayBuffer(base64) {
var binary_string = window.atob(base64);
var len = binary_string.length;
var bytes = new Uint8Array(len);
for (var i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i);
}
return bytes.buffer;
}
b64toBlob = (base64, type = 'application/octet-stream') =>
fetch(`data:${type};base64,${base64}`).then(res => res.blob())
displayPDF(data:any){
this.b64toBlob(response.base64Result).then((data) =>{
this.pdfViewerAutoLoad.pdfSrc = data;
this.pdfViewerAutoLoad.refresh();
}
component.html
<div style="height:120vh; width:100%;" *ngIf="this.pdfViewerAutoLoad.pdfSrc !== null">
<ng2-pdfjs-viewer #pdfViewerAutoLoad></ng2-pdfjs-viewer>
</div>
When I run the ngIf
I get the following error, "Cannot read property 'pdfSrc' of undefined". How do I hide the containing div when the viewer's source is empty?