Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No PDFJS.workerSrc specified error on using pdfjs-dist package from npm #8204

Closed
the-fallen opened this issue Mar 28, 2017 · 10 comments
Closed
Labels

Comments

@the-fallen
Copy link

the-fallen commented Mar 28, 2017

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

@the-fallen the-fallen changed the title No PDFJS.workerSrc specified on using pdfjs-dist package from npm No PDFJS.workerSrc specified error on using pdfjs-dist package from npm Mar 28, 2017
@the-fallen
Copy link
Author

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.

@itsarshkalsi
Copy link

Go to pdf.js

search function getWorkerSrc()

replace this lines

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

@Snuffleupagus
Copy link
Collaborator

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.

@luistak
Copy link
Contributor

luistak commented Aug 6, 2019

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;

  ...

@TroyWolf
Copy link

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! ☕

@danielmcq
Copy link

danielmcq commented Sep 24, 2019

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;

@luistak
Copy link
Contributor

luistak commented Sep 25, 2019

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

@danielmcq
Copy link

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. :)

@luistak
Copy link
Contributor

luistak commented Oct 9, 2019

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

#11220

@charlesfries
Copy link

Is it possible to add a TypeScript type definition to this package for the pdfjs-dist/build/pdf.worker.entry import?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants