Pdf.js: Cross-Domain file ?

Created on 24 Sep 2011  路  12Comments  路  Source: mozilla/pdf.js

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

1-other

All 12 comments

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

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.

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

@infantiablue were you able to figure it out?

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

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.

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?

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 !! :(

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.

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

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.

???

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?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sujit-baniya picture sujit-baniya  路  3Comments

brandonros picture brandonros  路  3Comments

kleins05 picture kleins05  路  3Comments

SehyunPark picture SehyunPark  路  3Comments

hp011235 picture hp011235  路  4Comments