react-pdf: Document cannot be rendered after update to v9.0.0
Before you start - checklist
- I followed instructions in documentation written for my React-PDF version
- I have checked if this bug is not already reported
- I have checked if an issue is not listed in Known issues
- If I have a problem with PDF rendering, I checked if my PDF renders properly in PDF.js demo
Description
After the latest update I started to get the following errors:
I am using cra.
Steps to reproduce
I am importing the module and the pdf worker as following:
import {Document, Page, pdfjs} from "react-pdf";
import 'react-pdf/dist/esm/Page/AnnotationLayer.css';
import 'react-pdf/dist/esm/Page/TextLayer.css';
import "./PDFViewer.scss";
import {TextItem} from "pdfjs-dist/types/src/display/api";
import {DocumentCallback} from "react-pdf/src/shared/types";
pdfjs.GlobalWorkerOptions.workerSrc = new URL(
'pdfjs-dist/build/pdf.worker.min.mjs',
import.meta.url,
).toString();
const options = {
cMapUrl: '/cmaps/',
standardFontDataUrl: '/standard_fonts/',
};
…
Expected behavior
No errors.
Actual behavior
The PDF document cannot be opened.
Additional information
No response
Environment
- Browser (if applicable): Chrome Android;
- React-PDF version: 9.0.0;
- React version: 18.2.0;
- Webpack version (if applicable): 5.91.0;
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 2
- Comments: 23 (2 by maintainers)
@pk-bt I have similar issue. When I run my application locally it doesn’t matter, but pdf file was not loaded after deploy.
I think this code is not working for some enviroment.
So I run below code on my terminal to copy workers bundle to my public folder
and modify above code like this.
after that it runs well in my deployed app.
I referenced this article https://pspdfkit.com/blog/2021/how-to-build-a-reactjs-viewer-with-pdfjs/ I hope this helps.
@pk-bt Were you able to solve this issue without having the worker being fetched from “unpkg”? I’m having similar issues with v9 except the error I’m getting is:
Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of “application/octet-stream”. Strict MIME type checking is enforced for module scripts per HTML specby add :
fixed this issue. https://github.com/storybookjs/storybook/issues/20157
It worked fine with the below mentioned code.
pdfjs.GlobalWorkerOptions.workerSrc = new URL( ‘pdfjs-dist/build/pdf.worker.min.mjs’, import.meta.url, ).toString();
and add the below mentioned MIME type to nginx.conf
application/javascript mjs;
Thank you @silvertae
I think I’ll try that solution, meanwhile I had to downgrade the version to make it work, fortunately I only needed to show a preview of the PDF, but maybe in a couple of weeks I’ll try that solution, thanks @ComplexRalex!
Just for the record, I managed to fix my error by writing this in my
nginx.config.I first tried with @jiangxiaoqiang’s solution, and I got the same problem as @javiertorresm2000. With some more research, I could find a solution to the problem (I’m new to
nginx, if you are wondering…)Thanks to this response in another issue! https://github.com/mozilla/pdf.js/issues/17296#issuecomment-2298713658.
This fixed it for me as well, thank you!
For some reason, pdfjs-dist version “4.4.168” is not being installed with react-pdf@latest, so I installed pdfjs-dist version “4.4.168” separately, and it works smoothly. ❤️
Page source here: https://github.com/pk-bt/react-pdf-example/ The page is deployed here: https://github.com/pk-bt/playground/ View it here: https://pk-bt.github.io/playground/
If I start the project locally in developer mode the component works without problems, I get errors on the production version.
I have noticed some lines at the beginning of the “pdf.worker.min.mjs” on the production that should not be there, in fact the files shown the error console.