Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phonegap InAppBrowser display pdf 2.7.0

I want to display an external PDF with the phonegap InAppBrowser in Android, but it isn´t working.

This is my JS code:

<script> function openPDF() {      var ref = window.open('http://www.i-drain.net/userfiles/file/einbauanleitung_iboard.pdf', '_blank', 'location=yes');      ref.addEventListener('loadstart', function(event) { alert('start: ' + event.url); });      ref.addEventListener('loadstop', function(event) { alert('stop: ' + event.url); });      ref.addEventListener('loaderror', function(event) { alert('error: ' + event.message); });      ref.addEventListener('exit', function(event) { alert(event.type); }); }   </script> 

I want to open the pdf after clicking on a image so i use this html code:

 <a href="#" onclick="openPDF()" >    <img src="images/button.png">  </a> 
like image 937
Marc Ster Avatar asked May 28 '13 14:05

Marc Ster


People also ask

What is InAppBrowser in Android?

The InAppBrowser is a web browser view that displays when calling [window. open](window. open. html)() , or when opening a link formed as <a target="_blank"> .

How do I close InAppBrowser?

There is no action to close the InAppBrowser. How can we achieve this? The user is redirected to an aws login page in the IAB and after suscessfull login the IAB should close and the user should be redirected to a screen in the app.

What is Cordova plugin InAppBrowser?

You can show helpful articles, videos, and web resources inside of your app. Users can view web pages without leaving your app. To get a few ideas, check out the sample at the bottom of this page or go straight to the reference content.


2 Answers

For everybody who is stuck with this problem here is what i´ve finally found out:

HTML 5 object tag: not working

Embedded tag: not working

childBrowser Plugin: I think it would work, but it is designed for the 2.0.0 version. So you´ll get a lot of errors so i didn´t get it to work.

Phonegap InAppViewer: If you use it like that:

window.open('http://www.example.com/test.pdf', '_blank', 'location=yes') 

it wont work. The InAppViewer can´t open PDF, it doesn´t matter if the PDF is external or local stored.

My solutions so far (not what the actual idea was):

you can call the window function with _system. like that:

window.open('http://www.example.com/test.pdf', '_system', 'location=yes') 

this will open the PDF in the normal Browser and download it. After downloading it will ask if it should open it with a PDF viewer. But you have to go back to your app and open it again after that.

Another possibility would be that you just download it to your sdcard with the Phonegap Filesystem API like that:

var fileTransfer = new FileTransfer(); fileTransfer.download( "http://developer.android.com/assets/images/home/ics-android.png", "file://sdcard/ics-android.png", function(entry) {     alert("download complete: " + entry.fullPath); }, function(error) {     alert("download error source " + error.source);     alert("download error target " + error.target);     alert("upload error code" + error.code); }); 

The last thing i´ve found out is, to use Google docs/drive to open it with the InAppViewer linked to google docs like that:

document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() {  window.open('https://docs.google.com/viewer?url=http://www.example.com/test.pdf&embedded=true', '_blank', 'location=yes');  ref = window.open('index.html', '_self');  } 

This will open the PDF in the app viewing it on google docs. You can generate your permalink here: https://docs.google.com/viewer So even if you change your pdf file, it will still work

I hope this summary will help you save time. If there is another solution let me know

like image 166
Marc Ster Avatar answered Oct 14 '22 01:10

Marc Ster


Android has no native PDf viewer capability in their browser (unlike iOS' Safari). As far as I can see there are two nice options:

  1. Skip downloading entirely and show the pdf via the online view capabilities of Google Doc Viewer, as stated in this StackOverflow answer

  2. DO download the file and use the FileOpener plugin for Android Phonegap apps as explained in this StackOverflow answer. This will either open the file in the installed PDF reader or present the user with a choice.

I've written some stuff on downloading and showing a PDF on iOS and these options I showed in my blog post for Android Phonegap developers.

like image 33
EeKay Avatar answered Oct 14 '22 01:10

EeKay