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
You can just put the Header set Access-Control-Allow-Origin * setting in the Apache configuration or htaccess file. It should be noted that this effectively disables CORS protection, which very likely exposes your users to attack.
The error you get is due to the CORS standard, which sets some restrictions on how JavaScript can perform ajax requests. The CORS standard is a client-side standard, implemented in the browser. So it is the browser which prevent the call from completing and generates the error message - not the server.
PDF. js is an open-source JavaScript PDF viewer that renders PDF using web standards-compliant HTML5. Primarily seen in Mozilla Firefox's as the built-in PDF viewer, PDF. js also serves as an easy way for developers and integrators to embed PDF viewing capabilities in a web app or server.
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:
.success(function(buffer){
PDFJS.getDocument(buffer);
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With