3

Using the viewer from pdf.js I successfully managed to open a local pdf file with the InAppBrowser. The following code works without flaws on iOS: the InAppBrowser opens and displays the pdf with the viewer!

window.resolveLocalFileSystemURL(fileURL, function (file) {
    window.open('lib/pdf.js-master/web/viewer.html?file=' + file.toURL(), '_blank', 'location=no')
}, fail);

file.toURL() renders to something like file:///blabla/bla.
However, on Android the pdf fails to load, giving the console error

"XMLHttpRequest cannot load file:///android_asset/www/lib/pdf.js-master/web/locale/locale.properties. Cross origin requests are only supported for HTTP.", source: file:///android_asset/www/lib/pdf.js-master/web/viewer.html?file=file:///storage/emulated/0/Android/data/xxxxxxxxx/cache/677.pdf

and there isn't even a www/lib/pdf.js-master/web/locale/locale.properties file... I don't know if this has anything to do with the problem, but I'm including it here.
But this is followed by the error

"XMLHttpRequest cannot load file:///storage/emulated/0/Android/data/xxxxxxxxx/cache/677.pdf. Cross origin requests are only supported for HTTP.", source: file:///android_asset/www/lib/pdf.js-master/web/viewer.html?file=file:///storage/emulated/0/Android/data/xxxxxxxxx/cache/677.pdf

which absolutely has something to do with the problem.
Resulting in the following screen. enter image description here

I don't know how to fix the error. And once again, it works flawlessly on iOS! What can I do to make it work on android? How can I enable cross origin requests for file:///? Is this the problem?
Thanks for any help.

Clawish
  • 2,934
  • 3
  • 24
  • 28
  • Does the file exist in the path ? – Raptor Apr 01 '15 at 01:44
  • It does! I can successfully do `file.moveTo(destination, 'newname');` – Clawish Apr 01 '15 at 01:47
  • I noticed you're on Simulator. Does this work on real Android device? – Raptor Apr 01 '15 at 01:49
  • `file.moveTo(destination, 'newname');` works, I know that from earlier this day. About the issue above, I don't know since I don't own an Android device myself. But my guess would be that it has nothing to do with emulation. – Clawish Apr 01 '15 at 01:51
  • Any chance you can try [this](http://stackoverflow.com/questions/19471577/how-to-open-a-pdf-file-that-is-in-local-file-system-cordova) ? – Raptor Apr 01 '15 at 02:10
  • I will try it and see if it works. But this is not the solution I want. I want the pdf to open *inside* the app, not with an external app. – Clawish Apr 01 '15 at 09:26
  • Yes this works. The file does exist! But once again, this is not the solution I want since this opens the pdf externally. – Clawish Apr 01 '15 at 09:52

1 Answers1

1

Have you explicitly enabled your webview to request files from a URL via javascript?

This works by default for Android API 15 and below. However, the default is set to false for API 16 and above, which means that you must call:

setAllowFileAccessFromFileURLs(true);

Here is a full example:

// create a webview with javascript enabled
WebView mWebView = (WebView) findViewById(R.id.webview);
mWebView.setWebChromeClient(new WebChromeClient());
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setAllowFileAccessFromFileURLs(true);

// finally load your pdf.js html file with the correct parameters
mWebView.loadUrl("file:///android_asset/pdfviewer/...");

Then to make it work with InAppBrowser, add the following after line 657 in src/android/InAppBrowser.java:

settings.setAllowFileAccessFromFileURLs(true);

You can also have a look at this issue for a different approach to override the WebView settings: Building custom WebView With Cordova 5.0 in Android

Community
  • 1
  • 1
jmeinke
  • 530
  • 6
  • 27