pdf.js: Console error "Setting up fake worker failed" while using in React

Attach (recommended) or Link to PDF file here: https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf

Configuration:

  • Web browser and its version: Chrome 83.0.4103.116
  • Operating system and its version: MacOS 10.14.6
  • PDF.js version: 2.4.456
  • Is a browser extension: No

Steps to reproduce the problem:

  1. Using credit-react-app to create a react project https://github.com/facebook/create-react-app npm init react-app my-app
  2. Install pdfjs-dist package npm install pdfjs-dist --save
  3. Import package and copy example code to App.js (https://github.com/mozilla/pdf.js/blob/master/examples/node/getinfo.js https://mozilla.github.io/pdf.js/examples/index.html#interactive-examples for prev/next example)
import pdfjsLib from 'pdfjs-dist/es5/build/pdf.js';
...
pdfjsLib.GlobalWorkerOptions.workerSrc = '../node_modules/pdfjs-dist/build/pdf.worker.js';
...
pdfjsLib.getDocument(url);
  1. Run react project yarn start
  2. Console shows error
Uncaught SyntaxError: Unexpected token '<' pdf.js:11915 
Uncaught (in promise) Error: Setting up fake worker failed: "Cannot read property 'WorkerMessageHandler' of undefined".
    at pdf.js:11915
Screen Shot 2020-07-06 at 9 49 47 PM

What is the expected behavior? (add screenshot) Worker is setting up correctly.

What went wrong? (add screenshot) pdfjsWorker does not exist in window object, which causes ‘WorkerMessageHandler’ of undefined issue. The switch statement started with case 0 -> case 3 -> case 6 -> case 8, ended up with catch() in _setupFakeWorker()

function setupFakeWorkerGlobal() {
...
case 8:
   return _context.abrupt("return", window.pdfjsWorker.WorkerMessageHandler);
...
}
Screen Shot 2020-07-06 at 9 50 15 PM Screen Shot 2020-07-06 at 9 50 32 PM Screen Shot 2020-07-06 at 10 01 58 PM

Link to a viewer (if hosted on a site other than mozilla.github.io/pdf.js or as Firefox/Chrome extension): (Trying to push code to my github, but reproducing the issue is very straight forward if following the steps. all functions are the same as example code. renderPage() etc.)

Screen Shot 2020-07-06 at 10 07 39 PM Screen Shot 2020-07-06 at 10 10 11 PM

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 3
  • Comments: 15

Most upvoted comments

Hi, I don’t know why, but looks like copy pdf.worker.js to my project’s output folder, or use cdn worker will make the magic happen. Using the one from node_modules/ doesn’t work.

 // pdfjsLib.GlobalWorkerOptions.workerSrc = '../node_modules/pdfjs-dist/build/pdf.worker.js';
  pdfjsLib.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/2.1.266/pdf.worker.js`;

https://github.com/wojtekmaj/react-pdf#create-react-app

Hi, the same problem happened in my project. I guess it may be related to webpack moudle, but I’m not sure. Do you have any solutions now? (I have to avoid using cdn)

Hi, I had the same issue, but managed to solve it. It doesn’t really have anything to do with webpack, but you do need to change your webpack configuration for it to work. Normally when you use the create-react-app it allows you to import files from the node_modules folder, however setting the workerSrc as in the following line doesn’t work. pdfjsLib.GlobalWorkerOptions.workerSrc = '../node_modules/pdfjs-dist/build/pdf.worker.js'; The reason for this is that you don’t use an actual loader to load this file, we pass a path and at runtime a worker is created from this path, but at runtime this file is nowhere to be found and the path also doesn’t exist… To solve this you can make use of webpack its asset modules. In your webpack configuration you will need to adapt some things in your module -> rules.

  • Exclude the worker file from your normal .js/.ts loader (probably the babel-loader) so that when you later import it, it is not treated as your normal js files and transpiled by babel (because you use a file that was build already) , see the exclude path: { test: /\.(js|mjs|jsx|ts|tsx)$/, include: paths.appSrc, exclude: /pdfjs-dist\/build\/pdf\.worker\.js$/, loader: require.resolve('babel-loader'), ... } Inside exclude we don’t prepend with node_modules as we will import it as ‘pdfjs-dist/build/pdf.worker.js’ later.

  • Add an asset loader for this specific file like this: { test: [/pdfjs-dist\/build\/pdf\.worker\.js$/], type: 'asset/resource' },

  • Next import the workerSrc in the file where you have to specify it: import workerSrc from 'pdfjs-dist/build/pdf.worker.js'

  • Use workerSrc that you just imported and assign it: pdfjsLib.GlobalWorkerOptions.workerSrc = workerSrc

This works when running it locally, haven’t tested this yet when running on a live environment. Hope this helps,

Kind regards

With React:

import workerSrc from 'pdfjs-dist/build/pdf.worker.entry';
pdfjsLib.GlobalWorkerOptions.workerSrc = workerSrc;

Hi, I don’t know why, but looks like copy pdf.worker.js to my project’s output folder, or use cdn worker will make the magic happen. Using the one from node_modules/ doesn’t work.

 // pdfjsLib.GlobalWorkerOptions.workerSrc = '../node_modules/pdfjs-dist/build/pdf.worker.js';
  pdfjsLib.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/2.1.266/pdf.worker.js`;

https://github.com/wojtekmaj/react-pdf#create-react-app