简体   繁体   English

如何使用Phonegap inAppBrowser预览不同的pdf文件?

[英]How to preview different pdf file using Phonegap inAppBrowser?

I have some question regarding to Cordova Phonegap inAppBrowser. 我对Cordova Phonegap inAppBrowser有一些疑问。 I have include the inAppBrowser to my code. 我已将inAppBrowser包含在我的代码中。

function onDeviceReady() {
     var ref = window.open('http://~/Testing.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); });
}

I using the code for preview the pdf file. 我使用代码预览pdf文件。 The question I wanted to ask is if I wanted to display multiple pdf file in my server, I have to create multiple html page with this code but different pdf file link?? 我想问的问题是,是否要在服务器中显示多个pdf文件,我必须使用此代码创建多个html页面,但使用不同的pdf文件链接?

I hope someone can help me in this because I'm new in this. 我希望有人能对此有所帮助,因为我是新来的。

如何使用像PDF.JS这样的javascript插件?

You can't use InAppBrowser to display a pdf because it's just a mean to get a WebView. 您不能使用InAppBrowser来显示pdf,因为这只是获取WebView的一种手段。

What you need to do is first to download the file via the plugin FileTransfer . 您需要做的是首先通过插件FileTransfer下载文件。

For that, you have to determine a file path on the device which vary over the OS. 为此,您必须确定设备上的文件路径,该文件路径因操作系统而异。 See File plugin and the system layouts to find a location with the desired level of privacy. 请参阅文件插件和系统布局以找到具有所需隐私级别的位置。 For that need, let's say cordova.file.dataDirectory for Android and cordova.file.documentsDirectory for iOS. 对于需要,比方说cordova.file.dataDirectory Android和cordova.file.documentsDirectory为iOS。

Then you have 2 options : 然后,您有2个选择:

  • open it via the plugin FileOpener2 . 通过插件FileOpener2打开它。 See Examples for opening a pdf resource 请参阅打开pdf资源的示例
  • or use PDF.js , a library written by Mozilla, to build your own HTML reader of a pdf file 或使用由Mozilla编写的库PDF.js构建自己的pdf文件的HTML阅读器

The first solution is far easiest but your app will depend of what pdf reader apps are installed on the user's device, which can vary from one device to the other. 第一种解决方案最简单,但是您的应用将取决于用户设备上安装的pdf阅读器应用,这在一个设备之间会有所不同。

The second solution gives you a finer control of your app and avoids to depend on other app but you will have to build a whole reader, and require to know such things like HTML canvas and JS promise. 第二种解决方案为您提供了对应用程序的更好控制,并且避免依赖其他应用程序,但是您将必须构建一个完整的阅读器,并且需要了解诸如HTML canvas和JS promise之类的知识。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM