-
Notifications
You must be signed in to change notification settings - Fork 10.2k
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! |
Thankyou @z0d14c, this worked for me. It does attach For Those coming from Nuxt or looking to extract text from a pdf, here is my utility function for thatimport "pdfjs-dist/build/pdf.worker.mjs";
// Function to extract text from a PDF file
export async function extractTextFromPDF(file: File): Promise<string[]> {
// dynamic import to avoid ssr issues
const { getDocument } = await import("pdfjs-dist");
// Read the file as an array buffer
const arrayBuffer = await file.arrayBuffer();
// Load the PDF document
const pdfDocument = await getDocument({ data: arrayBuffer }).promise;
const pagesText: string[] = [];
// Loop through each page
for (let i = 0; i < pdfDocument.numPages; i++) {
const page = await pdfDocument.getPage(i + 1); // Pages are 1-based in pdfjs
// Get the text content of the page
const textContent = await page.getTextContent();
// Extract and join the text items into a single string
const pageText = textContent.items
.map((item) => (item as any).str)
.join(" ");
pagesText.push(pageText);
}
return pagesText;
} |
Anyone Knows how to fix this with Vue3? Please I've been stuck on this for 1 week!
|
@alvaroman23 It's telling you what the problem is. It's trying to import a module from Have a look at this thread. vitejs/vite#11804 In one case it turned out to be an ad blocker that was preventing an import. However, in your case it looks like it's assuming that your path the node module is a URL. Also, look further up in this thread, because I posted a solution that I was using with Vue 3 + Vite. |
Same error here and searching for fix. |
So thank you, I apply this import in my reactjs project and it's successful :D |
At the writing of this, I think the right solution for integrating "vite": "^5.3.4",
"pdfjs-dist": "^4.4.168", import workerSrc from 'pdfjs-dist/build/pdf.worker?worker&url'
import * as pdfjs from 'pdfjs-dist'
pdfjs.GlobalWorkerOptions.workerSrc = workerSrc
pdfjs.getDocument('/bitcoin.pdf').promise.then((pdf) => {
console.log(pdf);
}) https://vitejs.dev/guide/assets.html#importing-script-as-a-worker Webpack should be able to also do something like this https://webpack.js.org/guides/web-workers/ |
For Vite + Sveltekit
|
For plain JS, I had to use the following code: import * as pdfjsworker from "pdfjs-dist/build/pdf.worker.mjs"
import * as pdfjs from "pdfjs-dist/build/pdf.min.mjs"
pdfjs.workerSrc = pdfjsworker;
let loadingTasks = pdfjs.getDocument(fileUrl);
let fileCheckPromise = await loadingTasks.promise; This gave me an pdfjs.GlobalWorkerOptions.workerSrc = workerSrc |
For React, I used the workerPort: "react": "^18.3.1",
"pdfjs-dist": "^4.10.38",
"vite": "^6.0.5" import * as pdfjsLib from 'pdfjs-dist';
import pdfjsWorker from 'pdfjs-dist/build/pdf.worker.mjs?worker';
pdfjsLib.GlobalWorkerOptions.workerPort = new pdfjsWorker();
export const extractTextFromPDF = async (file: File): Promise<string> => {
try {
const fileArrayBuffer = await file.arrayBuffer();
const pdf = await pdfjsLib.getDocument(fileArrayBuffer).promise;
... |
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: