Console error "Setting up fake worker failed" while using in React
See original GitHub issueAttach (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:
- Using credit-react-app to create a react project
https://github.com/facebook/create-react-app
npm init react-app my-app
- Install pdfjs-dist package
npm install pdfjs-dist --save
- 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);
- Run react project
yarn start
- 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
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);
...
}
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.)
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:6
Top GitHub 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.
https://github.com/wojtekmaj/react-pdf#create-react-app
Given that we know nothing about React here, is it meaningful to keep this issue open when we unfortunately cannot help? /cc @timvandermeij