5

I'm creating an lwc component that shows a pdf with canvas, the lib I'm using is this:PDF,js when trying to load lib, returns me an undefined error, can someone help me how to load correctly

enter image description here enter image description here

My Html

<template>
<lightning-card>
    <div class="container">
        <canvas id="the-canvas" class="canvas" lwc:dom="manual"></canvas>
    </div>
</lightning-card>    
</template>

My Js

import { LightningElement } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { loadScript } from 'lightning/platformResourceLoader';
import PdfViewerJs from '@salesforce/resourceUrl/pdfViewer';

export default class compC21Body extends LightningElement {

 renderedCallback() {
    const jsPath = PdfViewerJs + '/pdf.js';
    window.console.log(jsPath);

    loadScript(this, jsPath).then(() => {
        window.console.log('success');
    }).catch(error => {
        window.console.log(error);
        this.dispatchEvent(
            new ShowToastEvent({
                title: 'Error Pdf Viewer',
                message: error ? error.message : 'We get error to load some script :/',
                variant: 'error',
            }),
        );
    });
  }
}

I put the lib build folder inside the statistic feature in salesforce Inside the static resource folder we have the files

  • pdf.js
  • pdf.js.map
  • pdf.worker.js
  • pdf.worker.js.map

if you have any library suggestions, I just need it to use canvas and not iframe

error update
enter image description here SecureElement does not allow access to charset

enter image description here Failed to load script at /resource/1573759295000/pdfViewer/pdf.js: regeneratorRuntime is not defined [regeneratorRuntime is not defined]

13
  • Can you access your static resource if you hit the URL directly in your browser? Commented Nov 14, 2019 at 21:13
  • yes I tested, and it works :( Commented Nov 15, 2019 at 14:49
  • Besides seeing the error in the console of the browser (it looks like you are using Chrome), can you see if the actual resource is being loaded in the Network tab and see if it has the correct path? Also see if it is throwing a 200, 404 or 500 Commented Nov 15, 2019 at 14:51
  • 2
    This essentially means that the resource you are trying to load is not "Locker Compliant". You may want to load your resource in the Locker Console developer.salesforce.com/docs/component-library/tools/… and see where exactly it is throwing the issue Commented Nov 15, 2019 at 16:35
  • 2
    @AlefRibeiro would you mind posting them here as an answer to your question? Commented Feb 7, 2020 at 20:41

1 Answer 1

1

Try uploading just the pdf js file in a separate static resource and pdf worker js in a separate static resource and load pdf js first and worker js next.

import { LightningElement } from 'lwc';
import PDFJS from '@salesforce/resourceUrl/pdfJS';
import PDFWORKER from '@salesforce/resourceUrl/pdfWorker'
import { loadStyle, loadScript } from 'lightning/platformResourceLoader';
export default class PdfPoc extends LightningElement {
    scale = 1;
    connectedCallback(){
        Promise.all([loadScript(this, PDFJS), loadScript(this, PDFWORKER )]).then(()=>{
            console.log('PDF JS loaded');
            pdfjsLib.GlobalWorkerOptions.workerSrc = PDFWORKER;
            this.paintCanvas();
        }).catch((error)=>{
            console.log(' Error loading scripts '+ error);
        })
    }
5
  • 2
    Can you please share this.paintCanvas(); code as well ? Commented May 20, 2021 at 6:21
  • 2
    @Manjunatha V P could you please share this.paintCanvas(); code. I want to render pdf as image. You can answer a similar question here link Commented May 18, 2022 at 10:24
  • @manjunathavp - I got my PDFJS to work inside a lightning-container, which limits the application inside an iframe - did your PDFJS work outside of this limitation?
    – bkwdesign
    Commented Sep 9, 2022 at 14:40
  • This didn't work for me, I get the following error from console: WARNING: Failed to load script at /resource/1685230586000/PDFjsLibrary: Cannot set properties of undefined (setting 'pdfjsLib') [Cannot set properties of undefined (setting 'pdfjsLib')]
    – Emmanuel_M
    Commented May 31, 2023 at 15:38
  • When I try to print the error received from the .catch() I get undefined
    – Emmanuel_M
    Commented May 31, 2023 at 15:38

You must log in to answer this question.

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