Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDF.JS in Mobile apps Access-Control-Allow-Origin issue

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

like image 714
MobX Avatar asked Jul 25 '14 09:07

MobX


People also ask

How do I turn off access control allow origin?

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.

Is not allowed by Access Control allow Origin JavaScript?

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.

What is PDF JS viewer?

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.


1 Answers

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);  
})
like image 125
sibidiba Avatar answered Oct 05 '22 23:10

sibidiba