Pdf.js: No "GlobalWorkerOptions.workerSrc" specified.

Created on 21 Jan 2019  ·  4Comments  ·  Source: mozilla/pdf.js

if (!fallbackWorkerSrc && typeof document !== 'undefined') {
var pdfjsFilePath = document.currentScript && document.currentScript.src;
if (pdfjsFilePath) {
fallbackWorkerSrc = pdfjsFilePath.replace(/(.(?:min.)?js)(?.*)?$/i, '.worker$1$2');
}
}
Sometimes “document.currentScript” === null, pdfjsFilePath === null,

function getWorkerSrc() {
if (_worker_options.GlobalWorkerOptions.workerSrc) {
return _worker_options.GlobalWorkerOptions.workerSrc;
}
if (typeof fallbackWorkerSrc !== 'undefined') {
return fallbackWorkerSrc;
}
throw new Error('No "GlobalWorkerOptions.workerSrc" specified.');
}
image

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;

  ...

All 4 comments

You should always specify the workerSrc explicitly, i.e. by setting pdfjsLib.GlobalWorkerOptions.workerSrc before calling pdfjsLib.getDocument, since the fallback is only a best effort solution which is not guaranteed to work correctly in every situation.

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;

  ...

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;

  ...

I have had difficulty using the idea you gave in react
the problem is it doesnt work when the component is mounted
when I use the official script everything is fine but it doesnt work with pdfjs-dist

const pdfjsLib = window['pdfjs-dist/build/pdf
pdfjsLib.GlobalWorkerOptions.workerSrc = '//mozilla.github.io/pdf.js/build/pdf.worker.js';

any idea for that? I would prefer not to use the script

Sincerely I would use a react abstraction over pdf's since pdf.js officially doesn't support react :(

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kleins05 picture kleins05  ·  3Comments

zerr0s picture zerr0s  ·  3Comments

aaronshaf picture aaronshaf  ·  3Comments

timvandermeij picture timvandermeij  ·  4Comments

jigskpatel picture jigskpatel  ·  3Comments