Community
cancel
Showing results for 
Search instead for 
Did you mean: 
David_S_1
Beginner
403 Views

View embedded pdf with in-app-browser

Hi,

I'm trying to get an Android apk build to open a pdf embedded in the app (in the www directory with rest of content). This wasn't working in the standard webview, so I've tried in-app-browser plugin with the same result: the app launches an instance of an external pdf viewer (in this case Google pdf, which I believe is the same viewer used in Google Drive) within the in-app-browser window, then I get a message that the pdf can't be opened. I have been able to open a hosted pdf using the same href call (though half the time it just wants to download it). For the local file, I've tried href="pdf/myfile.pdf" and href="file://pdf/myfile.pdf/"

Here's my script:

$('a').click(function(e) {
	e.preventDefault();
	window.open($( this ).attr('href'), '_self', 'hardwareback=yes', 'location=no');
});

I know there's a plugin cordova-plugin-document-viewer, but this isn't a good solution for me because it relies on non-standard pdf viewers to be installed by the end user in the likely event they aren't already on board.

Any suggestions appreciated!

Thanks

Tags (2)
0 Kudos
5 Replies
PaulF_IntelCorp
Employee
403 Views

David -- I think the problem is that you're asking another app to open a file that is located in your app's private filesystem locale, that is, the folders and files where your app is stored are not available to other apps. See the doc page for the Cordova file plugin, which gives a quick overview of the filesystem on the devices. Short of creating a service that could provide the file via OS-specific methods, you could copy the file from your local app filesystem into one of the publicly accessible folders described on that page (using the file plugin) and then hand over that copied file to the browser.

David_S_1
Beginner
403 Views

Hi,

Thanks for the quick reply. This appears to be a more simple and direct solution: https://github.com/pwlin/cordova-plugin-file-opener2

I'll give it a try and let you know how it goes.

Thanks

Edit: ah, should have read the docs more carefully: this supports opening a file that already exists on the device filesystem. So I guess I'll have to circle back to the file plugin.

Anthony_H_Intel
Employee
403 Views

David-

Based on some of the comments that you and Paul made, it seems like both of you may have come across this blog post, but it seems worth sharing here for others:
https://www.raymondcamden.com/2016/06/26/linking-to-pdfs-in-cordova-apps/

It's a deep dive into how to get this working on Android and iOS with code samples.
In a nutshell, Paul's suggestion that you need to copy the pdf to a readable location is correct and the post has a code sample for how to do that.
There is some discussion in the comments of an alternative solution using PDF.js.

 

David_S_1
Beginner
403 Views

Hi Anthony,

No, I hadn't seen this. Though it covers some stuff I'd read in bits and pieces on stackoverflow (including pdf.js), this is the most complete discussion I've seen, including key differences between Cordova on iOS and Android.

I look forward to trying out the code.

Thanks!

David_S_1
Beginner
403 Views

I can confirm the Raymond Camden / José Antonio Postigo solution cited by Andrew does indeed work as advertised on a real device running Android 5.1.1. One gotcha that had me stumped for a wee bit is that some calls in the code quoted by Raymond depend on the File plugin which I had wrongly assumed was redundant to File Opener 2 and so had deleted.

Reply