question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot specify web worker src location (specifically prevents use of PDF.js)

See original GitHub issue

This issue has been generated from a comment by @gaearon in #1015. This issue will probably want to be labelled as a discussion. The objective of raising this issue is to capture a real-world use case to be resolved.

Essentially my objective is to be able to use PDF.js in my application. In order to do this I need to be able to configure the workerSrc attribute on the PDFJS global object.

The recommended approach (as described in this example) is to add a new entry into the webpack configuration so that a new bundle will be generated (just containing the worker source) and then pass a relative path to that bundle.

i.e. in webpack.config.js

  entry: {
    'main': './main.js',
    'pdf.worker': 'pdfjs-dist/build/pdf.worker.entry'
  },

then when using PDF.js

var pdfjsLib = require('pdfjs-dist');

// Setting worker path to worker bundle.
pdfjsLib.PDFJS.workerSrc = '../../build/webpack/pdf.worker.bundle.js';

Unfortunately #1084 deals with the requirement to add additional entries (which at the time of writing is still unresolved). I’ve raised this as a separate issue because although there is some overlap the is a specific requirement here to enable relative paths to JS files to be specified that can still be resolved as individual files after build… adding additional entries is one solution, but is entirely dependent upon webpack and my understanding is that a more generic solution (not specific to webpack) is required.

I’ve explored some other solutions (like using file-loader as described for this project) and have also explored using require.ensure.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

9reactions
draperdcommented, Feb 16, 2017

Another option I’ve just realised is to copy the worker source file to the public folder and reference is like this:

window.PDFJS.workerSrc = process.env.PUBLIC_URL + "/pdf.js/build/pdf.worker.js";

This also seems to work within the application, but for my specific use case (I’m using create react app as a development environment to build re-usable components that I’m then publishing to NPM) this won’t work… but as a solution just within create react app this would definitely work.

I’m happy to close this issue as my specific requirements are almost certainly outside the scope of this project.

3reactions
gaearoncommented, Feb 16, 2017

Thanks for raising this! It’s an interesting use case. We’ll keep this in mind when we consider supporting multiple entries.

Read more comments on GitHub >

github_iconTop Results From Across the Web

No PDFJS.workerSrc specified - Stack Overflow
I had a similar error and I fixed it by specifying the pdf.worker.js explicitly at the end of the pdf.js if (!PDFJS.workerSrc &&...
Read more >
Content Security Policy worker-src | by Krishna Chirumamilla
The main purpose of this directive is to prevent your already loaded scripts from loading more scripts in the form of workers.
Read more >
JSDoc: Module: pdfjsLib
src, GetDocumentParameters, Can be a URL where a PDF file is located, a typed array ... The worker that will be used for...
Read more >
Content-Security-Policy Header CSP Reference & Examples
The Content-Security-Policy header allows you to restrict how resources such as JavaScript, CSS, or pretty much anything that the browser loads. Although it...
Read more >
pdf2json - npm
PDF file parser that converts PDF binaries to text based JSON, powered by porting a fork of PDF.JS to Node.js. Latest version: 3.0.2,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found