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.');
}
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 :(
Most helpful comment
You should try this: