-
Notifications
You must be signed in to change notification settings - Fork 10.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The viewer.js can not take online PDF stream #1000
Comments
As referenced in the above issues this is something that the user will have to fix on their own using a proxy or CORS. |
Hi Brend, what's the main reason that "the user will have to fix on their own using a proxy or CORS"? It is important that the PDF could be coming from local (uploading), server as a file, server as stream like "http://". As image URL, the PDF reader should support HTTP URL. Thanks |
@jonasyuandotcom the cors allows you get the pdf using http from the same server. however the browser protects the user from getting/sending the data to foreign servers. Those servers must use http headers to bypass this restriction. Since your server side proxy will be located on same server as a viewer cors will be okay with that. |
@notmasteryet thanks. It works when using same server like var kDefaultURL = '/pdf-reader-web/sample.pdf'; |
Hi Jonas, We won't be implementing this on our end because it's not possible for us to implement because of browser security restrictions. See http://en.wikipedia.org/wiki/XMLHttpRequest#Cross-domain_requests Brendan |
Hi @brendandahl I was wondering if there is any update since 2011 ? Is it still impossible to fix the CORS problems ? |
Yes. See https://github.com/mozilla/pdf.js/wiki/Frequently-Asked-Questions#faq-xhr for more information. |
Thank you Tim ! |
Hi @timvandermeij. Thanks for your response. I've tried many solutions but I'm still not enable to allow CORS on my web server. Do you have any git example ? |
@Dassine here you go http://mozilla.github.io/pdf.js/web/viewer.html?file=//async5.org/moz/pdfjs.pdf -- PDF Viewer loads http://async5.org/moz/pdfjs.pdf . Notice that async5.org allows mozilla.github.io to get files. Otherwise a web browser has to block accessing a remote file for security reasons. It is a standard practice on the web and there is nothing PDF.js can do to circumvent browsers security. If you embedding browser control into desktop/mobile application, you can requests a binary data by using OS/Framework APIs and pass it to the PDF.js as Uint8Array. |
Thanks @yurydelendik for your links. I know that PDF.js doesn't manage CORS. I've tried the solutions sent by @timvandermeij and other but they failed. I'm looking for the right implementation/modifications solutions once the pdf.js repo downloaded. Thanks |
@yurydelendik I am also having the issue of loading remote PDF file. But this error happens only in Chrome. You can see that the PDF I try to load is on another server upon which I don't have any control. But still I wish to show this PDF on my viewer.js I have not much experience with CORS but I created a least security crossdomain.xml file on my server but even then it doesn't work. |
@hashbyte You will need a proxy for the server. A very simple proxy (developed by me) is CORS Anywhere. Simply prepend the URL of the proxy before the URL to the PDF file, e.g.
Then URL-encode this URL and put it in the Note: If the URL for the PDF does not contain any percent signs or Note: The CORS Anywhere demo is only provided to demonstrate the feature. If you are going to use this feature on a site with many visitors, please host the CORS Anywhere instance yourself, to avoid putting an unfair load on the public demo server. If I notice that the performance of CORS Anywhere is crawling due to abuse, your origin will be blacklisted. When hosting CORS Anywhere yourself, you can restrict access to your site only via the |
Hello, Error: file origin does not match viewer's please help! |
@gildassamuel see #6916 for details. |
@jonasyuandotcom In the case that the file is managed yourself, you can put the file and pdfjs on a same file server. |
@yjguoo The error message and the headers do not add up. I think that the redirect target is lacking the expected headers. Visit |
Hi Rob thanks for the quick response, I notice the when I manually input requested url "https://files.dev52.slack.com/files-pri/T076SHX5W-F07CGBKK2/git-for-beginners-handout.pdf" I get different response header from the file server: Thank you :) |
Yes, especially with cross-origin requests. When you perform a cross-origin request, the request is only accepted if the request is allowed by CORS. The browser will issue a request with the Also, by default credentials are not included in cross-origin requests. To include cookies, the server must respond with
I think that your server must be configured differently. See the documentation on MDN for more info: https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS |
After a cross-origin redirect, Chrome sets the Origin header to the "null" value instead of the actual URL - https://crbug.com/154967 You could conditionally return an |
Thank you! I'll look into it :) |
Hey Rob do you think there is another way to initialize the pdf viewer or pdf.js with the src already set and avoid XHR request all together? |
@yjguoo You can base64-encode the PDF data and use a data-URL. For large PDF files, this results in a worse user experience (=blank page without progress bar) because encoding the data as base64 increases the file size by 33%. I recommend to continue using XHR for this reason. |
Hi, just tested and it works if CORS is enabled, but what I see is that doesn't get the file in chunks/range if the file is very big. Solved adding this in .htaccess: Thank you!!! |
@Rob--W It was very helpful. There is any possibility from it to work from inside the project(like changing the XHR headers) ? (inside worker.js) |
Hi, for any one still having problems with this, I solved it with:
|
The viewer.js can not take online PDF stream.
By default, viewer.js has:
var kDefaultURL = 'compressed.tracemonkey-pldi-09.pdf';
Now use PDF stream:
http://www.liferay.com/documents/31578/11925632/sample.pdf
as
var kDefaultURL = 'http://www.liferay.com/documents/31578/11925632/sample.pdf';
In FireFox 9.0.1 and Chrome 16.0.912.63 , it throws errors:
"
PDF.JS Build: 9161c2e
Message: Unexpected server response of 0.
".
The text was updated successfully, but these errors were encountered: