Javascript 无法在 android 的 inappbrowser 中启动 pdf 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26304729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Not able to launch a pdf file in inappbrowser in android
提问by user3878988
I have a requirement to show a pdf in inappbrowser when user clicks on a link. It is working fine in ios but not working on android. I am using IBM worklight for my project. Below is the code I have used:
当用户单击链接时,我需要在 inappbrowser 中显示 pdf。它在 ios 上运行良好,但在 android 上不起作用。我在我的项目中使用 IBM Worklight。下面是我使用的代码:
window.open("pdfURL","_blank","location=yes");
In ios the inappbrowser launches and displays the pdf but in android the inappbrowser is launches but no content is displayed
在 ios 中,inappbrowser 启动并显示 pdf,但在 android 中,inappbrowser 启动但不显示任何内容
回答by Idan Adar
Unlike iOS, which has a built-in PDF viewer - Android's webview does not have PDF viewer built-in.
This is why it is going to fail in Android.
与具有内置 PDF 查看器的 iOS 不同 - Android 的 webview 没有内置 PDF 查看器。
这就是它在 Android 中失败的原因。
You have 2 choices in Android:
在 Android 中有 2 个选择:
View the file using Google Docs by storing the file at a remote serverand redirecting the user like so:
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 a Cordova plug-in. For example this one(you can search in Google for more). For this, you'll need to learn how to create Cordova plug-ins in Worklight.
通过将文件存储在远程服务器上并像这样重定向用户来使用 Google Docs 查看文件:
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');或者使用 Cordova 插件。例如这个(你可以在谷歌搜索更多)。为此,您需要学习如何在 Worklight 中创建 Cordova 插件。
You can read more options here: Phonegap InAppBrowser display pdf 2.7.0
您可以在此处阅读更多选项:Phonegap InAppBrowser display pdf 2.7.0
回答by coder2017
Try using
尝试使用
window.open('pdfURL',"_system","location=yes,enableViewportScale=yes,hidden=no");
where using _systemwill download the file and open it in the system's application like Chrome or other PDF viewers.
using_system将下载文件并在系统应用程序(如 Chrome 或其他 PDF 查看器)中打开它。
回答by Marc Magon
Use uriEncodeComponent(link) and https://docs.google.com/viewer?url=link
使用 uriEncodeComponent(link) 和https://docs.google.com/viewer?url=链接
Doc, Excel, Powerpoint and pdf all supported.
Doc、Excel、Powerpoint 和 pdf 都支持。
Use the cordova in app browser.
在应用浏览器中使用cordova。
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;
}
}
});
回答by Gideon Simons
Using google drive viewer seems like a good quick option.
使用谷歌驱动器查看器似乎是一个不错的快速选择。
But moving forward they may change or depreciate their API and you will have to find a new way to support this. Also security is another consideration here as your file is publicly available and downloadable.
但是继续前进,他们可能会改变或贬低他们的 API,你必须找到一种新的方法来支持这一点。安全性也是这里的另一个考虑因素,因为您的文件是公开可用的并可下载。
I had a similar situation in a recent project and we solved it by using: pdf.js
我在最近的一个项目中遇到了类似的情况,我们通过使用:pdf.js解决了它
This is a JS based PDF parser with a good set of reader functionalities.. its not as smooth as a native one but did the job for us. It's main advantage is that we had the control over the codes and were able to open files from the device file system.
这是一个基于 JS 的 PDF 解析器,具有一组很好的阅读器功能......它不像原生的那么流畅,但为我们完成了这项工作。它的主要优点是我们可以控制代码并能够从设备文件系统打开文件。
If you want to go for a more native like in-app route, then as @Idan Adar mentioned a native library + cordova plugin is the way to go.
如果您想采用更原生的应用程序路径,那么正如@Idan Adar 提到的那样,原生库+cordova 插件是您的最佳选择。
回答by Pankaj Singh Sugara
Try this code it's working fine 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);

