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 "GlobalWorkerOptions.workerSrc" specified. #10478
Comments
You should always specify the workerSrc explicitly, i.e. by setting |
You should try this:
|
I have had difficulty using the idea you gave in react
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 :( |
Please help, I still get this error when I use the library with svelte-kit |
What I did was, to append the script at the very beginning when my component is mounted.
when you use this, the I hope it helps, although I used it in React. |
Thanks for the information, so I cannot just import it like a regular package? |
no unfortunately, I tried it that way, it didnt work |
okey I think I know how to fixed it know |
I have been stuck with this problem using ver 2.4.456 of pdfjs-dist, checked the library webpack file at root |
has anyone faced error
|
I fixed it as follows:
|
@kvengl I have fixed it with simple solution. you can check #8305 |
People coming for ANGULAR fix: In imports: import * as PDFJS from 'pdfjs-dist';
// @ts-ignore
import pdfjsWorker from 'pdfjs-dist/build/pdf.worker.entry'
PDFJS.GlobalWorkerOptions.workerSrc = pdfjsWorker; and at the loading pdf end: private async loadPDF() {
const pdf = await PDFJS.getDocument(this.document.file).promise;
const page = await pdf.getPage(1);
const viewport = page.getViewport({ scale: 1 });
this.canvas.nativeElement.width = viewport.width;
this.canvas.nativeElement.height = viewport.height;
this.context = this.canvas.nativeElement.getContext('2d')!;
const renderContext = {
canvasContext: this.context,
viewport: viewport
};
await page.render(renderContext).promise;
this.isLoading = false;
} This will resolve your issue right away, but now you may face to resolve this update your imports for PDFjs to just: import * as PDFJS from 'pdfjs-dist';
PDFJS.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${PDFJS.version}/pdf.worker.js`; Angular: v15.1.0 |
For me, what seemed to work was this:
It appears that this code will attach the |
@z0d14c was right. Take a look file (typeof window !== "undefined"
? window
: {}
).pdfjsWorker = require("./pdf.worker.js"); the worker already assigned to import 'pdfjs-dist/build/pdf.worker.entry'; |
Everything went back to normal by doing it this way for me. import * as pdfjsLib from "pdfjs-dist";
// Import the worker correctly to avoid the message "Warning: Setting up fake worker"
import pdfjsWorker from "pdfjs-dist/build/pdf.worker.entry";
pdfjsLib.GlobalWorkerOptions.workerSrc = pdfjsWorker; the "Warning: Setting up fake worker" message came back, but I'll leave the idea in case it helps. |
// It will work Now use this import { pdfjs } from 'react-pdf'; pdfjs.GlobalWorkerOptions.workerSrc = //unpkg.com/pdfjs-dist@${pdfjs.version}/build/pdf.worker.min.js; |
Amazing, it worked for me on a ReactJS & TypeScript project! |
It's specified in the document. |
I came across this issue while trying to use
Which got past the initial error but still gets the |
Thanks a lot! This is the only solution that worked for me on React. |
This got my Angular app working. When I upgraded to Angular v17 and pdf.js v4, I had to change it to:
|
Has anyone figured out this error with pdf dist and angular 17 |
The solution from react-pdf the docs works and gets rid of the import * as pdfjs from 'pdfjs-dist'
pdfjs.GlobalWorkerOptions.workerSrc = new URL(
'pdfjs-dist/build/pdf.worker.min.mjs',
import.meta.url
).toString() However, I could see this encountering build issues unless you include the minified file in your build pipeline. |
Found a solution with Angular wich also resolves the
Then somewhere globally in your code (I do it in
Now the worker runs smoothly and no warning appears without using a CDN 🎉 |
This works on yarn, thank you! |
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.');
}
The text was updated successfully, but these errors were encountered: