react-pdf: sendWithPromise("GetPage", ... null causing an error
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
I use two instances of react-pdf. The main viewer is using a functional component and the second one is a class-based that’s not visible. It’s a background renderer to produce pdf thumbnail previews. This has been working fine since 2021.
After recently pulling down 5.7.2 my background PDF render throws an error just before it renders. After reverting to 5.7.1 it’s working fine.
- load 1 PDF it’s fine
- load another PDF it breaks
I put error handlers on the <Document...
and <Page..
. I have onSourceError
, onLoadError
, onRendeError
handlers but those don’t fire, the error above happens.
I will continue investigating/work on reproducible code unfortunately what I’m working on is not public.
Attached below are the images of the error.
Steps to reproduce
Setup a class-based react-pdf component.
Pass down a file path (by props), it renders.
Pass down another file path
Expected behavior
The PDFs render as the sources are updated.
Actual behavior
Fails to render the PDF/produces the errors below
When this error happens it crashes the entire react app eg. blank screen.
Additional information
No response
Environment
- Browser (if applicable): Chrome
- React-PDF version: 5.7.2
- React version: ^4.42.0
- Webpack version (if applicable): ^16.3.0
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 4
- Comments: 31 (5 by maintainers)
I think we solved the issue. It’s mostly in the user-code together with internal logic of component. Demo code is simple and does not handle file change situation. Solving for us was easy - just make page number of pages to render to 0 at the moment file is changed. Sample code below (not sure about 100% compiling, but the idea should be clear. For sure this might be solved on component level, then it needs to ignore pages when loading is in progress.
`import React, { useState } from ‘react’; import { Document, Page } from ‘react-pdf’;
function MyApp() { const [numPages, setNumPages] = useState(0); const [fileName, setFileName] = useState(‘somefile.pdf’); const [pageNumber, setPageNumber] = useState(1);
function onDocumentLoadSuccess({ numPages }) { setNumPages(numPages); // <== here is the issue #1 - we save page number of file, but in case it’s changed, we know about new number only after it’s loaded, before doc is loaded, this value is wrong }
function setNewFile(){ //file change event setNumPages(0); setFileName(‘newfile.pdf’); }
return ( <div onClick={setNewFile}> <Document file={fileName} onLoadSuccess={onDocumentLoadSuccess}> {Array.from(new Array(numPages), (_, index) => ( <Page key={index} pageNumber={index + 1} /> ), )} { /* here we render all pages of file, but if new file is being loaded, we try loading pages that are not available */ } </Document>
Page {pageNumber} of {numPages}
</div> ); }`We’ve hit the same situation with an application using react 16.14. The first PDF seemed to load fine, but remounting or changing to a new PDF would result in errors.
Was able to workaround by rolling back to 5.7.1 as others have. Don’t have much else to add to previous reports, but did want to note that the root cause was one of a number of different errors on different subcomponents from Page down. For future search-ability, figured it was worth adding examples from a dev bundle created with 5.7.2:
We have this issue for the following flow:
我在添加自定义cMapUrl的时候也报这个错,之后把option给缓存就不会有这个问题了。
We’re having the same issue on v6.2.0. Reverting to v5 fixes it. We haven’t been able to reproduce locally but it does happen in production and we’re getting many errors. We re-render with different files many times during the component’s lifetime.
Not a solution but I’ve capped the
react-pdf
version in my project to5.7.1
for the time being.I am facing the same issue when using 7.3.3.
Hello @WoldemortRules
According to the documentation onLoadSuccess -> Function called when the document is successfully loaded.
The error can be temporarily bypassed by using:
I found a related issue in react-pdf-viewer that was due to reusing a destroyed worker:
It seems that we’re encountering this bug when changing loading blob A then blob B very quickly. My guess is that when page 1 of blob A gets to load, the worker for blob A is already destroyed because blob B is now loading.