Pdf.js: No PDFJS.workerSrc specified error on using pdfjs-dist package from npm

Created on 28 Mar 2017  路  9Comments  路  Source: mozilla/pdf.js

test

function usage(blob) {
    console.log('usage called!');
    var fileReader = new FileReader();
    fileReader.onload = function (blob) {
        require('pdfjs-dist');
        var fs = require('fs');
        PDFJS.getDocument(blob.target.result).then(function (pdfDocument) {
        console.log('Number of pages: ' + pdfDocument.numPages);
        });
    }
    fileReader.readAsArrayBuffer(blob);
}

implemented as shown in the example for node, but throws error for not specifying workingSrc.
I'm using content scripts for a chrome extension which disallow document.currentscript as fcfort commented in this issue

1-other

Most helpful comment

You should try this:

  const pdfjs = await import('pdfjs-dist/build/pdf');
  const pdfjsWorker = await import('pdfjs-dist/build/pdf.worker.entry');

  pdfjs.GlobalWorkerOptions.workerSrc = pdfjsWorker;

  ...

This absolutely worked for me, except I'm using native import.

import pdfjs from 'pdfjs-dist';
import pdfjsWorker from 'pdfjs-dist/build/pdf.worker.entry';

pdfjs.GlobalWorkerOptions.workerSrc = pdfjsWorker;

All 9 comments

As fcfort commented, it works when i use

PDFJS.workerSrc = chrome.extension.getURL("libs/pdf.worker.js");
before calling PDFJS.document and put my pdf.worker.js in root folder(root-directory/libs/pdf.worker.js).
But this isn't a clean solution. I hope there's a fix for this.

Go to pdf.js

search function getWorkerSrc()

replace this lines

pdfjsFilePath = "YOUR_PATH_TO_JS_FILE/pdf.worker.js";
if (pdfjsFilePath) {
return pdfjsFilePath;
}

But this isn't a clean solution. I hope there's a fix for this.

That's not a work-around, but actually the correct solution!
You should always set the workerSrc option before calling getDocument, which is even mentioned in the docs, to ensure that the worker file will be loaded correctly.

Please keep in mind that even though we attempt to obtain a fallback path, there's no simply no guarantee that it will be successful if the workerSrc option wasn't explicitly set by the user.

Closing as answered.

You should try this:

  const pdfjs = await import('pdfjs-dist/build/pdf');
  const pdfjsWorker = await import('pdfjs-dist/build/pdf.worker.entry');

  pdfjs.GlobalWorkerOptions.workerSrc = pdfjsWorker;

  ...

But this isn't a clean solution. I hope there's a fix for this.

That's not a work-around, but actually the _correct_ solution!
You should _always_ set the workerSrc option before calling getDocument, which is even mentioned in the docs, to ensure that the worker file will be loaded correctly.

Regarding that documentation. I'm trying to follow it. I'm not God's gift to programming, but I'm not always the dumbest guy in the room either. I am having a difficult time making heads or tails of this paragraph in the documentation:

RE: https://github.com/mozilla/pdf.js/wiki/Setup-pdf.js-in-a-website

To use the library in your project add require('pdfjs-dist') to your file requires and build your project normally. The worker shall be built into a separate bundle: take the file "./node_modules/pdfjs-dist/build/pdf.worker.entry.js" or built a separate file that uses require('pdfjs-dist/build/pdf.worker'). PDFJS.workerSrc shall be set to point to this file. You can use the pdfjs-dist/webpack module for PDF.js autoconfiguration.

It seems to have a typo at least, but even if that is fixed, this is not very clear to me. It might reduce some frustration and wasted time answering questions if more explanation can be put around this bit.

Thanks! :coffee:

You should try this:

  const pdfjs = await import('pdfjs-dist/build/pdf');
  const pdfjsWorker = await import('pdfjs-dist/build/pdf.worker.entry');

  pdfjs.GlobalWorkerOptions.workerSrc = pdfjsWorker;

  ...

This absolutely worked for me, except I'm using native import.

import pdfjs from 'pdfjs-dist';
import pdfjsWorker from 'pdfjs-dist/build/pdf.worker.entry';

pdfjs.GlobalWorkerOptions.workerSrc = pdfjsWorker;

You should try this:

  const pdfjs = await import('pdfjs-dist/build/pdf');
  const pdfjsWorker = await import('pdfjs-dist/build/pdf.worker.entry');

  pdfjs.GlobalWorkerOptions.workerSrc = pdfjsWorker;

  ...

This absolutely worked for me, except I'm using native import.

import pdfjs from 'pdfjs-dist';
import pdfjsWorker from 'pdfjs-dist/build/pdf.worker.entry';

pdfjs.GlobalWorkerOptions.workerSrc = pdfjsWorker;

i have preferred import with an await to split the bundle, this lib is kinda heavy

Yeah. Either way. The concept is the same. I'm just glad I found your comment because it helped me resolve this problem rather quickly. :)

I added a simple create-react-app example on this PR

https://github.com/mozilla/pdf.js/pull/11220

Was this page helpful?
0 / 5 - 0 ratings