I have a lwc in which I try to render a pdf. I have zipped the pdfjs library and added this as a static resource. In my template I can call the library as following:
<iframe id="pdfFrame" src="/resource/pdfjs/pdfjs/web/viewer.html" width="100%" height="100%" class="pdfFrame"
onload={generatePdf}></iframe>
In my javascript controller I cant seem to create the pdf since the console.log only debugs the first "Alert 1"
Javascript
import {LightningElement, track} from 'lwc';
import {loadScript} from "lightning/platformResourceLoader";
import PDFJS from '@salesforce/resourceUrl/pdfjs';
export default class JspdfDemo extends LightningElement {
@track isModalOpen = false;
openModal() {
this.isModalOpen = true;
// this.generatePdf();
}
closeModal() {
this.isModalOpen = false;
}
submitDetails() {
this.isModalOpen = false;
}
renderedCallback() {
Promise.all([
loadScript(this, PDFJS)
]);
}
generatePdf() {
console.log('alert 1');
const { PDFjs } = window.pdfjs;
console.log('alert 2');
const doc = new PDFjs({
encryption: {
userPermissions: ["print", "modify", "copy", "annot-forms"]
}
});
console.log('alert 3');
doc.text('Hello World!', 20, 20);
var base = doc.output('datauristring').split(',')[1];
console.log('alert 4');
this.template.querySelector('iframe').contentWindow.postMessage(base, window.location.origin);
console.log('base: ' + base);
console.log('doc: ' + doc);
console.log('alert 5');
}
}
I do not get an error. What am I missing? Thanks in advance.
UPDATE after adding a try catch
TypeError: Cannot destructure property 'PDFjs' of 'window.pdfjs' as it is undefined.