Could not start pdf file in inappbrowser in android

I have a requirement to show pdf in inappbrowser when the user clicks on the link. It works fine in ios, but does not work on Android. I use IBM to work on a project. Below is the code I used:

window.open("pdfURL","_blank","location=yes"); 

In ios, inappbrowser launches and displays pdf, but in android inappbrowser starts, but the content is not displayed

+6
source share
5 answers

Unlike iOS, which has a built-in PDF viewer - Android Android browser does not have a built-in PDF viewer.
That is why it will fail in Android.

You have 2 options in Android:

  • View the file using Google Docs, saving the file to a remote server and redirecting it like this: window.open(encodeURI('https://docs.google.com/gview?embedded=true&url=http://www.your-server.com/files/your_file.pdf'), '_blank', 'location=yes,EnableViewPortScale=yes');

  • Or use the Cordova plugin. For example this one (you can search on Google more). To do this, you need to learn how to create Cordova plugins in Worklight .

Here you can read more options: InAppBrowser phone screen display pdf 2.7.0

+12
source

Try using

 window.open('pdfURL',"_system","location=yes,enableViewportScale=yes,hidden=no"); 

where using _system will download the file and open it in a system application such as Chrome or other PDF views.

+2
source

Using a google drive viewer seems like a quick feature.

But moving forward, they can change or depreciate their API, and you will need to find a new way to support this. In addition, security is another consideration as your file is publicly available and downloadable.

I had a similar situation in a recent project, and we solved it using: pdf.js

This is a JS-based PDF parser with a good set of read functions. He is not as smooth as his own, but he did the work for us. The main advantage is that we controlled the codes and could open files from the file system of the device.

If you want to switch to a more native one, as in the application, then, as @Idan Adar mentioned the native library + cordova plugin, this is the way to go.

+1
source

Use uriEncodeComponent (link) and https://docs.google.com/viewer?url= link

Support for Doc, Excel, Powerpoint and pdf.

Use the cord in the application browser.

 document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() { window.open = cordova.InAppBrowser.open; } $("body").on("click",function(e){ var clicked = $(e.target); if(clicked.is('a, a *')) { clicked = clicked.closest("a"); var link = clicked.attr("href"); if(link.indexOf("https://") !== -1) { if(true) //use to be able to determine browser from app { link = "http://docs.google.com/viewer?url=" + encodeURIComponent(link) + "&embedded=true"; } window.open(link, "_blank", "location=no,toolbar=no,hardwareback=yes"); return false; } } }); 
+1
source

Try this code, it works great for me.

 var inAppBrowserRef; var target = "_system"; var options = "location=yes,hidden=no,enableViewportScale=yes,toolbar=no,hardwareback=yes"; inAppBrowserRef = cordova.InAppBrowser.open(url, target, options); 
0
source

Source: https://habr.com/ru/post/976580/


All Articles