0

I have the below HTML

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

JS:

    generatePdf() {
        const { jsPDF } = window.jspdf;
        const doc = new jsPDF({
            encryption: {
                userPermissions: ["print", "modify", "copy", "annot-forms"]
            }
        });
        
        doc.text('Hello World!', 20, 20);

    var base = doc.output('datauristring').split(',')[1];

    this.template.querySelector('iframe')
        .contentWindow.postMessage(base, window.location.origin);
}

It renders as below

enter image description here

There is no download or print button available on the PDF viewer. could someone please suggest how to get these buttons on the screen?

1 Answer 1

1

If you want to download the file based on a link, you can add a custom lightning button and then add below code that should download the file for you.

<lightning-button variant="brand" label="Download" title="Primary action" onclick={handleClick} class="slds-m-left_x-small"></lightning-button>

in your JS :

handleClick(event){
    let downloadLink = document.createElement("a");
                downloadLink.setAttribute("type", "hidden");
                downloadLink.href = "data:application/octet-stream;base64,"+this.pdfData; // PDF base64Data
                downloadLink.download = "filename.pdf";//modify as you need
                document.body.appendChild(downloadLink);
                downloadLink.click();
                downloadLink.remove();
}

this is what I could make it work from StachExchange only, I am having hard time finding that thread to link here.

Also, for Print you dont need to do anything differently, using cmd + p in mac works for me to get print pop up.

6
  • @gs650x did it work for you? May 5, 2021 at 3:36
  • I am sorry, I am in IST hours, I just saw your answer, I have tried this, thank you for your kind help but our application is mainly focused on mobile users, above works as expected on desktop but doesn't work on salesforce 1 mobile app, could you please suggest similar workaround which will work on mobile phones?
    – gs650x
    May 5, 2021 at 4:12
  • I dont have experience with mobile app since it will be much different than this approach. You can however do this, a dirty approach will be to Save the PDF to contentDocument (the one you are displaying to the users) - get the DownloadableUrl from contentDocument object - delete the file from ContentDocument once user downloads it. May 5, 2021 at 4:15
  • yes, this is the only approach, I could think of as of now, thank you for your help.
    – gs650x
    May 5, 2021 at 4:16
  • By any chance can you please help me with the jsPDF-autotable I have Imported it but still it throws autoTable is not a function
    – gs650x
    May 5, 2021 at 8:45

You must log in to answer this question.

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