1

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.

6
  • wrap the whole code of generatePdf in a try-catch block. It will give you the specific errors. Commented Jan 10, 2023 at 10:19
  • Check this detailed answer, it may help salesforce.stackexchange.com/questions/343331/…
    – Saroj Bera
    Commented Jan 10, 2023 at 10:27
  • @MukulMahawariya I get the following error: TypeError: Cannot destructure property 'PDFjs' of 'window.pdfjs' as it is undefined.
    – Thomas
    Commented Jan 10, 2023 at 10:32
  • print window object in the console and search for pdfjs. You might be calling the wrong name or it's not yet initialized. Commented Jan 10, 2023 at 10:41
  • @SarojBera I have ideed looked at this example. Thee issue in this ticked is that the code is not consistend its using a different library jspdf and pdfjs between template and controller
    – Thomas
    Commented Jan 10, 2023 at 10:42

1 Answer 1

0

For anyone who bumpes into this

  <iframe id="pdfFrame" src="/resource/pdfjs/pdfjs/web/viewer.html" width="100%" height="100%" class="pdfFrame"
            onload={generatePdf}></iframe>

Is calling the pdfjs library which contains viewer.html.

const { PDFjs } = window.jspdf; Note: jspdf instead of pdfjs is calling jspdf library that resolves error TypeError: Cannot destructure property 'PDFjs' of 'window.pdfjs

1
  • Are you saying here that even though the library name is pdfjs, the window call has to be to jspdf? Commented Nov 23, 2023 at 0:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .