rsm23's avatar
Level 14

Uncaught ReferenceError: PDFJS is not defined

I want to use Mozilla's PDFjs on my Laravel project but it seems that's I didn't find the proper way to implement it, I've installed it, and require it on my app.js file :

require('pdfjs-dist/build/pdf');
PDFJS.workerSrc = require('pdfjs-dist/build/pdf.worker');

But I keep this error on my console :

Uncaught ReferenceError: PDFJS is not defined

can any one help me with this issue.

0 likes
5 replies
rohu2187's avatar

Hey rsm23, were you able to configure PDFJS with laravel?

rapa's avatar

For those who use Vite follow the below

// Import the library
import * as pdfjsLib from "pdfjs-dist";
// Import worker
import pdfJSWorkerURL from "pdfjs-dist/build/pdf.worker?url";
// Import whatever modules you need
import { PDFSinglePageViewer, EventBus, PDFLinkService, PDFFindController, PDFScriptingManager } from "pdfjs-dist/web/pdf_viewer";
// Bind the worker for later use 
window.pdfJSWorkerURL = pdfJSWorkerURL;

Then in the component assign the worker

pdfjsLib.GlobalWorkerOptions.workerSrc = pdfJSWorkerURL;
stromhurst's avatar

@rapa Hi. Do I put this in the resources/js/app.js

and in the component, you mean in the js script we want to use the pdfjsLib in?

Please or to participate in this conversation.