Before you start - checklist
What are you trying to achieve? Please describe.
In our project (issue, demo), I'd like to load only the pages that I'm viewing, and render the first page before the entire document is loaded.
From my understanding, PDF.js supports Range headers and the react-pdf
API describes that it's possible to include a PDFDataRangeTransport
object in the file
property. I fail to see what to do to actually send these Range headers, though!
Describe solutions you've tried
Environment
Hi,
Yeah, PDFDataRangeTransport should be supported, as React-PDF just passes it to pdf.js, does not much else with it. I found this topic on PDFDataRangeTransport objects creation.
It seems like the easiest way to get the behavior you want is to simply pass an URL as file
prop. This should work just fine: https://github.com/mozilla/pdf.js/wiki/Frequently-Asked-Questions#range
Thanks for the reply (and the awesome library, for that matter) @wojtekmaj!
Unfortunately, I do pass the URL as a file prop (source, demo), but it only renders after the entire document has been fetched.
Also, the request for the PDF file does not appear have any range headers.
Perhaps this conditional is never actually true if I pass a string?
// File is PDFDataRangeTransport
if (file instanceof PDFDataRangeTransport) {
return { range: file };
}
According to PDF.js developers, PDF.js does not support gzip
encoding of range responses, so it needs to be set explicitly. According to the PDF.js docs, you can set custom headers. Since Document passes the options
object to PDFjs.getDocument
, this should work:
<Document
options={{
httpHeaders: {
'Accept-Encoding': 'Identity',
}
}}
file={"https://example.com/some.pdf"}
>
However, it does not, so I'm still investigating what is going on. It seems likely that it's a pdf.js issue.
Most helpful comment
According to PDF.js developers, PDF.js does not support
gzip
encoding of range responses, so it needs to be set explicitly. According to the PDF.js docs, you can set custom headers. Since Document passes theoptions
object toPDFjs.getDocument
, this should work:However, it does not, so I'm still investigating what is going on. It seems likely that it's a pdf.js issue.