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

What should be the value for workerSrc? #8305

Closed
yinso opened this issue Apr 17, 2017 · 23 comments
Closed

What should be the value for workerSrc? #8305

yinso opened this issue Apr 17, 2017 · 23 comments
Labels

Comments

@yinso
Copy link

yinso commented Apr 17, 2017

Link to PDF file (or attach file here):

Configuration:

  • Web browser and its version: Chrome 57.0.2987.133
  • Operating system and its version: OSX 10.11.6
  • PDF.js version: 1.7.365
  • Is an extension: bundled via webpack 2.2.1

I tried to follow the documentation for webpack but it's not working (keep getting setting up fake worker), probably due to me not understanding what exactly should go into workerSrc.

Is workerSrc:

  • a URL that'll get resolved by the browser? e.g. PDFJS.workerSrc = '/pdf.worker.js';
  • a Node require spec that'll get resolved and bundled into the same file via webpack & node?
    e.g. PDFJS.workerSrc = 'pdfjs-dist/build/pdf.worker.js';
  • a relative path that'll get resolved and bundled into the same file via webpack & node?
    e.g. PDFJS.workerSrc = '../../node_modules/pdfjs-dist/build/pdf.worker.js';
  • something else?

Thanks for any pointers.

@evadne
Copy link
Contributor

evadne commented Apr 30, 2017

@yinso should be something the browser can hit. You may not want to use a relative path since it may break if anything else in your stack makes asset digests. It is best if you render that from your web framework.

@timvandermeij
Copy link
Contributor

Moreover, you can refer to the Webpack example at https://github.com/mozilla/pdf.js/tree/master/examples/webpack. Closing as answered.

@yinso
Copy link
Author

yinso commented May 5, 2017

Thanks guys for the help - appreciated.

@luistak

This comment was marked as outdated.

@llamamoray
Copy link

I think @luistak solution should be added to the documentation. It's the cleanest way to load the worker in using webpack/create-react-app

@Mageenz
Copy link

Mageenz commented Sep 28, 2020

It's the cleanest way to load the worker in using webpack/create-react-app

@gremo
Copy link

gremo commented Sep 29, 2020

@luistak solution is good but still getting the error in console: Warning: Setting up fake worker.

Any help?

@luistak
Copy link
Contributor

luistak commented Sep 29, 2020

@luistak solution is good but still getting the error in console: Warning: Setting up fake worker.

Any help?

This was reported here and I don't really know much about pdf.js to fix 😞
I'm sorry

@Nataliasem
Copy link

I use Pdfjs v2.8.335 with legacy supporting in this way:

import * as pdfjsLib from 'pdfjs-dist/legacy/build/pdf'
import PDFJSWorker from 'pdfjs-dist/legacy/build/pdf.worker.entry'

pdfjsLib.GlobalWorkerOptions.workerSrc = PDFJSWorker

@matheus-brisance
Copy link

matheus-brisance commented Aug 24, 2021

I use Pdfjs v2.9.359 with legacy supporting in this way:

import * as PDFJS from 'pdfjs-dist/legacy/build/pdf';
import PDFJSWorker from 'worker-loader?esModule=false&filename=[name].[contenthash].js!pdfjs-dist/legacy/build/pdf.worker';

PDFJS.GlobalWorkerOptions.workerPort = new PDFJSWorker();

export default PDFJS;

@iamrmin
Copy link

iamrmin commented Jan 3, 2022

has anyone faced error FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory when using

import { pdfjs } from 'react-pdf'
import pdfjsWorker from 'pdfjs-dist/build/pdf.worker.entry'

pdfjs.GlobalWorkerOptions.workerSrc = pdfjsWorker

react-scripts build and react-scripts start just crashes when error mentioned above. most importantly it only occur when i enable source map.

@Johnnybar
Copy link

Johnnybar commented Jan 14, 2022

Having the same issue as @iamrmin. The solution looks straightforward but crash follows...
Only way around it is to set worker this way:
pdfjsLib.GlobalWorkerOptions.workerSrc = "//cdnjs.cloudflare.com/ajax/libs/pdf.js/{installed_npm_version}/pdf.worker.js";
But seems counter-intuitive...

@iamrmin
Copy link

iamrmin commented Jan 14, 2022

@Johnnybar Yes and I fixed it the same way 🤝

@Johnnybar
Copy link

Hi @iamrmin and whoever runs into this problem in the future. I managed to solve this issue by creating an .env file in the project's root where I deactivated building sourcemaps.
Here are links to 3 possible solutions:
https://stackoverflow.com/a/63745434
wojtekmaj/react-pdf#97 (comment)

@rajkp1997
Copy link

pdfjs.GlobalWorkerOptions.workerSrc = pdfjsWorker
has anyone faced error FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory when using

import { pdfjs } from 'react-pdf'
import pdfjsWorker from 'pdfjs-dist/build/pdf.worker.entry'

pdfjs.GlobalWorkerOptions.workerSrc = pdfjsWorker
react-scripts build and react-scripts start just crashes when error mentioned above. most importantly it only occur when i enable source map.

@lamnouar
Copy link

You have the issue because you already have an instance for the pdfWorker created, I hade the same issue and I fixed it by adding this globalThis.pdfjsWorker = null; just before setting the workerSrc
Like this you will be able to create a new instance with your local file

@ar2pi
Copy link

ar2pi commented Sep 2, 2022

If anyone reaches this point and uses this library in Node.js with Typescript, Serverless Framework and serverless-plugin-optimize here's what worked for me.

In your serverless.yml file:

provider:
  optimize:
    external:
      - pdfjs-dist/legacy/build/pdf.worker.js

In your code:

import * as pdfjs from 'pdfjs-dist/legacy/build/pdf';

pdfjs.GlobalWorkerOptions.workerSrc = 'pdfjs-dist/legacy/build/pdf.worker';

@zhaoxu-lingoace
Copy link

zhaoxu-lingoace commented Nov 15, 2022

Adding this to the entry file is automatically set workerSrc

import 'pdfjs-dist/webpack';

Official documentation

@xsh3byx
Copy link

xsh3byx commented Jun 19, 2023

me encontre con el mismo problema, y solucione asi:

import {getDocument, GlobalWorkerOptions, isPdfFile, version} from 'pdfjs-dist';
GlobalWorkerOptions.workerSrc = //cdnjs.cloudflare.com/ajax/libs/pdf.js/${version}/pdf.worker.js;

Espero que a alguien le sirva :)

@leungyauming
Copy link

For Vite users, you may try the following:

import * as pdfJS from "pdfjs-dist";
import pdfJSWorkerURL from "pdfjs-dist/build/pdf.worker?url";
pdfJS.GlobalWorkerOptions.workerSrc = pdfJSWorkerURL;

See: https://vitejs.dev/guide/assets.html#explicit-url-imports

@isacvale
Copy link

We're not allowed to rely on cloudfare's cdn, so we needed another approach.
I got this from react-pdf's README , and it worked ok.

pdfjs.GlobalWorkerOptions.workerSrc = new URL(
  'pdfjs-dist/legacy/build/pdf.worker.min.js',
  import.meta.url,
).toString();

@chtmiley
Copy link

chtmiley commented Feb 9, 2024

Adding this to the entry file is automatically set workerSrc

import 'pdfjs-dist/webpack';

Official documentation

zhaoxu-lingoace's solution worked for me using webpack. I did it following the instructions on worker loading in timvandermeij's example: https://github.com/mozilla/pdf.js/tree/master/examples/webpack

Worker loading

If you are getting the Setting up fake worker warning, make sure you are importing pdfjs-dist/webpack.mjs which is the zero-configuration method for Webpack users. Installing worker-loader is no longer necessary.

import * as pdfjsLib from 'pdfjs-dist/webpack.mjs';

@ahmedelhaw
Copy link

import 'pdfjs-dist/webpack';

fantastic

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