wojtekmaj / react-pdf

Display PDFs in your React app as easily as if they were images.
https://projects.wojtekmaj.pl/react-pdf
MIT License
9.11k stars 867 forks source link

customTextRenderer not called on blob URLs? #1650

Closed jsong-eq closed 9 months ago

jsong-eq commented 9 months ago

Before you start - checklist

Description

Hi, I read up docs and the recipes for text highlight.

I'm on version "react-pdf": "^7.3.3",

I'm using highlight text recipe as-is. It works if I test with a local pdf file - as

                         <Document file={"/public/sample.pdf"}>

text highlighting works. However, I'm rendering with url generated from blob - I do not see the text customTextRenderer called at all.

      api
        .getDocImage({
          uri,
        })
        .then((response: any) => response.blob())
        .then((blob: Blob | MediaSource) => {
          setPDFfile(blob); // type is application/pdf
          setPDFfile(URL.createObjectURL(blob)); // generated URL 
        });

I have tried highlight recipe on local pdf - that works great. However, trying to use customTextRenderer on a return pdf blob like above, customTextRender is not called.

Steps to reproduce

  1. Copy code from highlight text recipe
  2. Get a pdf file from blob
  3. save the blob as pdf OR createObjectURL (both do not work)
  4. confirm customTextRenderer is not called

Expected behavior

CustomTextRenderer should be called

Actual behavior

CustomTextRenderer is ignored on Page component.

Additional information

image

Is sample screenshot to show pdf blob.

Environment

wojtekmaj commented 9 months ago

Is there a chance you could create a reproducible example e.g. on CodeSandbox?

jsong-eq commented 9 months ago

https://codesandbox.io/s/pdf-js-createobjecturl-forked-ktlxkg?file=/src/App.js:0-2337

Is the closest I could get - but having trouble getting blob url working on codesandbox. Do you maybe have an example lying around that should work with blob URLs? If so, I should be able to quickly patch it in above ^

Thanks for checking the issue!

jsong-eq commented 9 months ago

Just on looking onto more - TextLayer is failing on blob pdf.

image

image

Is the error I get, will update if I learn some more.

jsong-eq commented 9 months ago

I'm actually suspecting that TextLayer gets lost during PDF file -> bytestream -> blob -> react-pdf process, but not sure how I can prove

wojtekmaj commented 9 months ago

"TextLayer task cancelled" sounds to me like something caused the TextLayer to update mid-render. Now, what could it be... 🤔

jsong-eq commented 9 months ago

Yeah I doubt that this is actually a problem from the library itself - mb! At least until I can prove that the issues is from the react-pdf - I'll close this out. Thanks