Skip to content
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

Cross-Domain file ? #522

Closed
infantiablue opened this issue Sep 24, 2011 · 13 comments
Closed

Cross-Domain file ? #522

infantiablue opened this issue Sep 24, 2011 · 13 comments
Labels

Comments

@infantiablue
Copy link

Hi there,

Thank you for creating such an interesting open-source library. I am wondering if this support to call file cross domain. Such as, in the web view example, I set the kDefaultURL = http://domainB.com/file.pdf then I call that viewer.js from domainA.com and it didn't work.

Is there anyway to solve this issue ?

Thank you very much

@brendandahl
Copy link
Contributor

Hello,

The PDF file is requested through a XMLHttpRequest so all the usual cross site scripting rules apply. There are a number of ways to get around this such as a proxy running on domainA see http://en.wikipedia.org/wiki/XMLHttpRequest#Cross-domain_requests for more options.

Brendan

@joneschrisg
Copy link
Contributor

Also, if the server at domainB sets the HTTP CORS headers properly (https://developer.mozilla.org/en/http_access_control#Access-Control-Allow-Origin) then the cross-origin request will work in modern browsers.

@infantiablue
Copy link
Author

Thank you very much for your explanations. I tried to set up and it works for the simple example, which use the ajax method

var ajaxGet = function(url, callback){
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.mozResponseType = xhr.responseType = 'arraybuffer';
xhr.expected = (document.URL.indexOf('file:') === 0) ? 0 : 200;
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === xhr.expected) {
var data = (xhr.mozResponseArrayBuffer || xhr.mozResponse ||
xhr.responseArrayBuffer || xhr.response);

  callback(data);
}

};
xhr.send(null);
}

However, in the advanced sample (like the web demo), it still didn't work. I think that the problem came from the function to call the file, it didn't support the binary type buffer. Could you guys to show me more ? I am no very good at javascript.

Thank you again

@brendandahl
Copy link
Contributor

@infantiablue were you able to figure it out?

@firespeier
Copy link

How does the google doc viewer work around this problem?...probably a stupid question..

@brendandahl
Copy link
Contributor

I'm not exactly sure but their doc viewer uses lots of server side processing so they don't have the same restrictions as a browser only program.

@firespeier
Copy link

Does anybody know if there is a way to use the client's connection. E.g. download the file to a temp directory (with any kind of script) and then load it with pdf.js? I guess thats not possible because of security issues, he? Would it be possible with a certificate or so?

@shinedezign
Copy link

Hi

I have the same issue ! I am uploading the pdf from one domain and displaying it in another domain. I am getting this error message

PDF.JS Build: undefined
Message: Unexpected server response of 0.

How to solve this cross domian issue ?

I tried to add to a server's HTTP response headers but does not work. Any help !! :(

@gigaherz
Copy link
Contributor

You need to allow Cross-origin Requests in the PDF hosting domain, so that it allows requests from the viewer domain.

If you can't do that, then you can't do Cross-origin requests, because of security concerns.

@taxilian
Copy link

taxilian commented Aug 7, 2014

Here is how I solved this issue without changing pdf.js: http://colonelpanic.net/2014/08/using-pdf-js-web-worker-cross-domain-cors/

@fabi0w
Copy link

fabi0w commented Apr 25, 2015

XMLHttpRequest cannot load http://www.cesp.com/my.pdf. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.

???

@superlc0413
Copy link

I have a list of PDF filenames, I want to load the corresponding PDF when clicking one of them.How can i do this with PDFJS?

@ravi0999
Copy link

I have found the solution in asp.net c-sharp first convert cross domain pdf file into base64 string and then bind it into iframe.

here is the solution : https://www.dontnetcodes.com/2021/03/How-to-Bind-a-cross-domain-pdf-file-into-pdf.js-viewer-pdf.viewer-issue-solution-in-asp.net-using-csharp.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

10 participants