26

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>
Marc Ster
  • 2,276
  • 6
  • 33
  • 63

8 Answers8

63

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

Marc Ster
  • 2,276
  • 6
  • 33
  • 63
  • I think you can remove the "&embedded=true" in the google viewer line? It's requiring me to login if I put it there. – thedjaney Oct 18 '13 at 05:20
  • '_system' was what I wanted the entire time and everyone kept trying to open files in the browser... Thumbs way up and great detailed answer – Erik Grosskurth Dec 23 '15 at 15:13
14

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.

Community
  • 1
  • 1
EeKay
  • 6,494
  • 3
  • 24
  • 24
8

Try in this way it will work.

var ref = window.open('http://docs.google.com/viewer?url=http://www.i-drain.net/userfiles/file/einbauanleitung_iboard.pdf', '_system', 'location=yes');
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Elius
  • 81
  • 1
  • 1
5

The answer of Marc Ster is very comprehensive. However, the approach to open the pdf with google docs requires the user to login with their google account.

If you are displaying pdfs that are located on a server that is your own you can avoid this and use the following approach: You can setup the PDF.js library on your server. It is the open source project that also google docs are based on to view PDFs in a web browser.

You can then use the InAppBrowser Plugin to head onto your server where the PDF.js lies, specifying a path for the pdf to display in the URL. The pdf should also be on your server, to avoid having CORS issues. If it is an external pdf, you could write a workaround, as stated here.

You might display a pdf client side with code similar to this (using the InAppBrowser plugin)

var fileName = "myPdfOnMyServer.pdf";
var url = encodeURIComponent('files/uploads/' + fileName);
var ref = window.open(
         'https://www.<my website>.com/pdfjs-1.0.10XX-dist/web/viewer.html?file='
         + url,
         '_blank',
         'location=no,closebuttoncaption=Close,enableViewportScale=yes');
Clawish
  • 2,934
  • 3
  • 24
  • 28
2

Android webview does not have PDF viewer built-in. Redirect user to Google doc viewer example: http://docs.google.com/viewer?url=http://example.com/example.pdf

lsolano
  • 129
  • 1
  • 2
1

Try this to open any kind of documents from URL using following steps:

It works for me both on Android and IOS. I used it for open images and PDF files.

Android : It opens files using system apps if available, otherwise it give an error, which you can handle.

IOS : It opens files in popup like view with Done button and Option button.

It doesn't show your docs URL.

Source is available here : https://github.com/ti8m/DocumentHandler

SANAT
  • 8,489
  • 55
  • 66
0

I tried it having the script in the same file of the html and it worked for me, the only change was that i added the "alt" attribute at the image:

 <html lang="en">

    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        <a href="#" onclick="openPDF()" >
   <img alt="aaa" src="images/button.png">
 </a>
    </body>
</html>
<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>
SamDroid
  • 601
  • 1
  • 10
  • 28
  • tried it with the same file it isn´t working :/ is something else important? I also set the rights for the plugin.. nothing happens even if i want to call the function with "device ready" – Marc Ster May 28 '13 at 18:10
  • Do you have two activity in your android manifest?You need it to make it working.Try calling the window.open function in the onclick event. E.g onclick="window.open('whatever url')" and let me know the result.If it won't work, follow this one: [link](http://stackoverflow.com/questions/15666258/phonegap-build-open-external-page-in-inappbrowser-or-childbrowser-with-no-tool)@MarcSter – SamDroid May 29 '13 at 07:05
  • what do you exactly mean with "two activity in you android manifest"? I just added this permission line : to the config.xml, like described in the phonegap api. – Marc Ster May 29 '13 at 12:09
  • @MarcSter: can you try to "execute" the last full example of this page [PhoneGapApi](http://docs.phonegap.com/en/2.7.0/cordova_inappbrowser_inappbrowser.md.html) and tell me if it works? – SamDroid May 29 '13 at 12:32
  • The last example at least opens the pdf viewer. But the external pdf i want to display isn´t showing.. – Marc Ster May 29 '13 at 12:55
  • It doesn't open also replacing its url in the example? @MarcSter – SamDroid May 29 '13 at 13:00
  • i tried this code: function onDeviceReady() { // external url var ref = window.open(encodeURI('http://www.i-drain.net/userfiles/file/einbauanleitung_iboard.pdf'), '_blank', 'location=yes'); // relative document ref = window.open('indexE.html', '_self'); } it opens the inAppViwer and i am also able to click on "done" to get back to the index site.. but the pdf is not showing – Marc Ster May 29 '13 at 13:07
  • Did you add the external site to your whitelist? Can you make regular `window.open()` calls to some random page (not a .pdf) on that domain? – MBillau May 29 '13 at 19:29
  • normal page works well! But pdf isn´t working either external nor internal. Thank you very much for your help! Really appreciate it – Marc Ster May 30 '13 at 10:01
  • Googling I discover that Android doesn't have PDF viewer for their Webview, get a look to this question:[GoogleDoc](http://stackoverflow.com/questions/2655972/android-webview-pdf) – SamDroid May 30 '13 at 10:32
  • even youtube videos work with the InAppViewer.. Are there differences between the pds formats which work? I don´t know what to do... – Marc Ster May 30 '13 at 10:46
  • @SamDroid but isn´t that the actual use of the InAppViewer displaying pdf? – Marc Ster May 30 '13 at 10:47
  • If you use the normal browser you can download the pdf by clicking on the link. Is this possible in the phonegap app, too? This would be ok.. – Marc Ster May 30 '13 at 10:53
  • Yes, this is possible in phonegap, see [Documentation] (http://docs.phonegap.com/en/2.7.0/cordova_file_file.md.html#FileTransfer) – SamDroid May 30 '13 at 10:56
  • Thank you! I successfully downloaded the pdf from the external link now. I saved it to /sdcard... next problem is, how can i save it to a place the user can find it easily? Bad idea if he has to search it with a explorer.. can i open it with the "FileReader" ? Will it display the pdf then with the installed pdf reader? – Marc Ster May 30 '13 at 11:15
  • It depends on you, some PdfREader find every pdf file in the memory, otherwise there isn't another way to find it without search it.. – SamDroid May 30 '13 at 11:18
  • Phonegap Api FileReader says "Files can be read as text or as a base64 data encoded string" .. i guess pdf isn´t a text – Marc Ster May 30 '13 at 11:19
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/30900/discussion-between-samdroid-and-marc-ster) – SamDroid May 30 '13 at 11:21
0
function openPDF() {
     var ref = window.open('http://docs.google.com/viewer?url=http://www.i-drain.net/userfiles/file/einbauanleitung_iboard.pdf', '_system', '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); });
}
Pranav Singh
  • 17,079
  • 30
  • 77
  • 104
Richa
  • 1