-
Notifications
You must be signed in to change notification settings - Fork 10.1k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Content-Length header exposed in CORS configuration for remote docs breaks PDF viewer #4530
Comments
Good news, this isn't a spooky problem in network.js, it's all a matter of CORS configuration. For the javascript viewer to display a remotely-hosted pdf document, the following configuration values are necessary: Access-Control-Allow-Origin "whatever.hostname.you.have.or.*.for.all.of.them" The Allow-Headers setting is necessary to let the xhr specify what range it wants - without this, the flow for the xhr's secondary request (after discovering that range requests are supported and the original GET should be canceled in favor of the partial content loading) stalls out at the OPTIONS preflight request. Once this setting is in place, the CORS workflow gets underway. The Expose-Headers setting is necessary for the browser to include enough of the restricted headers into the response for the xhr to get the data to determine that a) range requests should be the way forward because the content is big enough to warrant it, and b) what byte range was received in the subsequent partial content requests. Adding these settings into the apache configuration mentioned above allows the test url with the mozilla viewer.js above to work. |
Really cool. Thanks for looking into this. |
@chrisblizm Sorry, for being late but in my case the pdf embedded directly via an
So if its an issue of CORS, why is this different in both cases? Both the files are hosted on the same server (localhost). Also I tried it on Firefox which uses PDF.js by default. Even there the issue is same. Am I missing something or doing it wrong? |
For those who have stumbled on this issue via Google, a working CORS configuration for S3 as per @chrisblizm's reply is:
The key is Access-Control-Allow-Headers "range". |
Thanks very much this solution worked for me. |
I'm having this issue (The pdf is not shown until the entire document is downloaded and the browser shows this message: "Refused to get unsafe header Accept-Ranges") but im using Azure Blob Storage. |
Did anyone deal with this issue using box.com? |
@simoncpu thank you for your findings! { 'Content-Length': 5751405,
'Content-Type': 'audio/mpeg',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',
'Access-Control-Allow-Headers': 'Range',
Expires: 0,
Pragma: 'no-cache',
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Accept-Ranges': 'bytes',
'Content-Range': 'bytes 120429-240237/5751405' } |
Where should i add this? Inside the viewer.html head tag? |
This resolved my issue, thank you @chrisblizm. It was easy to implement in the Azure Portal for Blob Storage. |
Loading a remote pdf document with the viewer will not succeed if the content-length header is exposed to the browser by the remote server's CORS configuration (if the document is larger than twice the chunk size).
A local apache installation was used to demonstrate this issue with the pdfjs project viewer:
http://mozilla.github.io/pdf.js/web/viewer.html?file=//localhost/pdfjs/PDF32000_2008.PDF
The document used in testing is located here: http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/PDF32000_2008.pdf
If the apache CORS configuration does not provide the 'expose the Content-Length header' instruction (Access-Control-Expose-Headers "content-length, accept-ranges") then the behavior of the viewer is to download the entire document before drawing any pages on the display.
If the configuration is changed to expose those headers (content-length and accept-ranges) then the behavior of the viewer is to abort the initial GET request in favor of creating a new GET specifying ranges, but only an OPTIONS preflight request is sent and response received and no further processing of the remote resource happens with the end result being no document is displayed at all.
This issue affects PDFJS.version = '0.8.1314' (generic build).
The text was updated successfully, but these errors were encountered: