-
-
Notifications
You must be signed in to change notification settings - Fork 902
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
pdf.worker.js returns Error 404 #97
Comments
Hey @benoj, sorry for the late reply. There's nothing in your webpack config that raises my concerns. Is there any file in your output folder with |
@wojtekmaj no worries - I have worked out the issue but not sure how to resolve it? The app works fine when I load |
@benoj I don't know whether it's a pretty solution, but if it only affects your devServer, you can use devServer proxy for re-directing requests that are going wrong way. |
@wojtekmaj no it affects the production app also. The app is a single page app using react router. When I open the app at the non root level then the request for the worker goes to the wrong path. Basically could be fixed by using an absolute path to Also do you know how the hash is calculated? And if it is constant? Ben |
Hmmm... I believe that all files dynamically imported by Webpack share the same logic, so you might have a problem with all bundles downloaded dynamically while being anywhere else but in |
I have updated my webpack (above) with the following:
but i can see the but confused as to how to solve this? my webpack seems fairly standard - not sure why other people are not seeing this? |
Update: Got it working. Would be keen to hear back on your opinion of this solution: webpack updated to build my own pdf.worker
setting workerSrc manually to the js output path
|
Hmmm, not bad workaround at all! By the way, support for setting your own PDFJS settings in a slightly more convenient way is coming soon. |
Hey @benoj, import { setOptions, <WhateverElseYouNeed> } from 'react-pdf';
setOptions({
workerSrc: '/js/pdf.worker.js',
}); Hope you like it! |
@wojtekmaj Awesome will give it a go! Thanks :) |
Not sure this is working as intended. I resorted to @benoj original solution as a work around. |
The important thing to note is that entry.webpack sole purpose is to enable PDF.js worker with zero configuration from your side. If you plan to disable it, you should not use entry.webpack, just a normal entry. |
I do not want to disable it, just have a bit more control over how it is being dynamically imported/referenced. I presume // importing these on their own
import Document from 'react-pdf/build/Document';
import Page from 'react-pdf/build/Page';
// configuring pdfjs
const pdfjs = require('pdfjs-dist');
pdfjs.PDFJS.workerSrc = '/js/pdf.worker.js'; EDIT: Looking at the source, think my problem was importing from import { Document, Page, setOptions } from "react-pdf/build/entry";
setOptions({
workerSrc: "/js/worker.pdf.js"
}); |
That looks okay to me ;) That if course requires you to do more configuration because you need to copy the loader yourself. By the way, that's default entry point so you can just write |
Facing this issue myself. ReactPDF is trying to load the worker at a relative path rather than absolutely against the domain. Anyone got any ideas how to sort it? I'm not all too familiar with webpack, so am using Laravel Mix |
Also ended up here -- my app serves static assets from |
@joshbrw, @tjwebb, please refer to the comment above. You don't need to force Webpack to put files anywhere you don't want them. I believe this is exactly what you are looking for - it should tell React-PDF where to look for the worker file. |
The workout around in above comment doesn't work anymore as
It however does not work, as the path I see being tried in DevTools is still relative to the current url and is not absolute. |
We are using this at our work to render PDF within our react app. It works fine locally, whether we use 'react-pdf' import, or 'react-pdf/dist/entry.webpack' or even On production, using 'react-pdf' import, pdf.worker.js is called from URL/pdf.work.js and is (status) "cancelled". Then we tried using the CDN and the file successfully loads, but in both situations, our tab freezes and eventually crashes. There is a message in devTool Sources "paused before potential out-of-memory crash". We were forced to take it out, no idea what is going on. I have read through the following issues: 371, 378 - no answer, even 8305 on pdf.js |
Any updates?
|
hi @RobRendell |
@nickjohngray I can't use Ideally, I'd like to work out how to add the proper types. I'm trying to extend the module declaration by adding a .d.ts file:
... but that didn't seem to fix the problem for me. I'm not proficient enough in Typescript to know what's going on with that. |
i got a few deadlines to achieve right now. i can help out when i finish
that stuff . the issue must be small , just an annoying type issue .
i noticed in the pass for libs that dont fully support typescript it was
not worth my time working out types for them . my time is limited and i
spend it it on building and fixing kick ass components. i surgest you do
the same . 😀
Thanks Nick.
…On Mon, 28 Sep 2020, 8:01 pm Rob Rendell, ***@***.***> wrote:
@nickjohngray <https://github.com/nickjohngray> I can't use any as a type
(it's an import statement), but I can precede the import with a line saying //
@ts-ignore to ignore the error. When I do that, everything works fine.
Ideally, I'd like to work out how to add the proper types. I'm trying to
extend the module declaration by adding a .d.ts file:
import {pdfjs, Document, Page, Outline} from 'react-pdf';
declare module 'react-pdf/dist/umd/entry.webpack' {
export {pdfjs, Document, Page, Outline};
}
... but that didn't seem to fix the problem for me. I'm not proficient
enough in Typescript to know what's going on with that.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#97 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AD3TSIQUIASADKB6UASTNRDSIAYEZANCNFSM4ECOS2TQ>
.
|
For anyone trying to enable it in Next.js with webpack5 Tell webpack to treat pdf.worker.js as an asset ...
webpack(config) {
config.module.rules.push({
test: /pdfjs-dist\/build\/pdf\.worker\.js$/,
type: "asset/resource",
generator: {
filename: "static/chunks/[name].[hash][ext]",
},
});
}
... import it in your code and set URL manually import { FC } from "react";
import { Document, Page, pdfjs } from "react-pdf";
import url from "pdfjs-dist/build/pdf.worker";
pdfjs.GlobalWorkerOptions.workerSrc = url; |
Dear Friends, after I spent some time with this issue here are my solutions that worked. I always had a typescript situation. Probably you can mention this thread in the docu. Pointing to the cloudsource is rather not an option for many companies. If you have a free configurable webpack config this is what worked. Second time a had a create react app setup and didnt want to eject, so I hosted the min file just in the public folder of CRA
Then in your code
|
This comment has been minimized.
This comment has been minimized.
For anyone looking for another solution for Typescript complaining about the import statement. Try to add this at the top of your tsx file. It worked for me.
|
This issue is stale because it has been open 90 days with no activity. Remove stale label or comment or this issue will be closed in 14 days. |
Great <3 |
Any solutions for vite users?
|
Vite user here as well ... or a workaround for "pdf.worker.js 404 (Not Found)"? Thx |
@mleister97 @guygit First class Vite support just landed on main and will be released with the next beta and eventually in 6.0.0 release. |
This issue is stale because it has been open 90 days with no activity. Remove stale label or comment or this issue will be closed in 14 days. |
This issue was closed because it has been stalled for 14 days with no activity. |
If you are struggling in react (web app) - like I was! Just use the following: if (typeof window !== 'undefined' && !pdfjs.GlobalWorkerOptions.workerSrc) { Will work fine. Best, Joseph |
Hi,
When i try to use the webpack.entry import my app crashes and I can see a network request going to the current path/
cba38462455a43d162b5.worker.js
which is returning 404.I am importing
import { Document, Page } from 'react-pdf/build/entry.webpack'
Here is my webpack config:
The text was updated successfully, but these errors were encountered: