I am trying to develop an app for mobile devices using Sencha and Cordova. As PDf support is not available in the browsers of Android I decided to use PDF.JS. It is working fine while loading the local PDf files, but when tring to open remote files it is throwing an error
http://<remote server pdf location>. Origin file:// is not allowed by Access-Control-Allow-Origin
Please let me know how to fix this issue. Is there any way to fix this in PDF.JS. I need PDF.Js files locally only as the app needs offline capability also.
Thanks in advance
Rather than calling
PDFJS.getDocument
with a URL, first get the binary data via a XMLHttpRequest, and then pass the result to thegetDocument
call in place of the URL. This will avoid the problem that seems inherent to the PDFJS library when running within a Cordova app.The following code example is taken from the pdfJs example found here: https://bitbucket.org/butelo/pdfviewer/downloads . The issue can be resolved by making the following changes in
customview.js
Replace this code:
With this code:
So that you will call:
Instead of:
Issue is occurring when PDF.js is using WebWorkers to download the document. CORS in WebWorkers is a complete mess across browsers. (CORS is a mess IMHO anyway.)
The usual recommendation will not work:
<access origin="*" subdomains="true" />
Solution: do the ajax yourself, with response type arraybuffer, then feed response to PDF.js: