简体   繁体   English

通过JavaScript以编程方式“在Adobe Reader中打开”

[英]Programmatically “open in Adobe Reader” from JavaScript

The iOS safari browser has a handy option that appears whenever you open a PDF page in the browser iOS Safari浏览器有一个方便的选项,只要您在浏览器中打开PDF页面,它就会出现

If you have Adobe Reader installed, a button will appear that says: 如果安装了Adobe Reader,将出现一个按钮,显示:

Open in "Adobe Reader"... 在“ Adob​​e Reader”中打开...

However, if you open up the PDF document where the HUD (address bar, etc) is hidden, like in a phone gap application, or a quick and dirty "Add to Home Screen app" using the following meta: 但是,如果您打开隐藏了HUD(地址栏等)的PDF文档,例如在电话缺口应用程序中,或者使用以下元数据在快速且肮脏的“添加到主屏幕应用程序”中:

<meta name="apple-mobile-web-app-capable" content="yes" />

then, obviously, the button will not appear. 那么,显然,该按钮将不会出现。

I want to still open the PDF document in Adobe Reader. 我仍想在Adobe Reader中打开PDF文档。 Does anyone know a way to do this programmatically? 有人知道以编程方式执行此操作的方法吗?

I want to include jsPDF in a phonegap application, and save the results, and this would be the easiest way to do that. 我想将jsPDF包含在phonegap应用程序中,并保存结果,这将是最简单的方法。

Thanks in advance! 提前致谢!

With some fantastic help from Vince Parsons (and others), I've solved this problem. 在文斯·帕森斯(和其他人)的帮助下,我已经解决了这个问题。

Using a PhoneGap Plugin, you can create / expose a JavaScript call, which is then processed in Objective-c. 使用PhoneGap插件,您可以创建/公开一个JavaScript调用,然后在Objective-c中对其进行处理。

Here's the two lines you need (yes, only two lines!), and a subsequent explanation: 这是您需要的两行(是的,只有两行!),以及随后的说明:

self.docInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:myDocumentPath]];
[self.docInteractionController presentOpenInMenuFromRect:CGRectMake(0,100,1,1) inView:UIApplication.sharedApplication.keyWindow animated:YES];

Explanation 说明

Your input will be the file location. 您的输入将是文件位置。 In my case it's inside the app sandbox, so it looks something like: 就我而言,它位于应用程序沙箱中,因此看起来像:

/var/mobile/Applications/B16-HU83-GU1D-1D3NT1F13R/Documents/DocumentToExport.pdf

If you want to use an external URL, you can (with adjustments), but for my purposes it was internal. 如果要使用外部URL,可以(进行调整),但是出于我的目的,它是内部的。

So, with my file location as an input (let's call my input variable myDocumentPath ), you just need to declare a document controller: 因此,以我的文件位置作为输入(我们将我的输入变量myDocumentPath称为),您只需要声明一个文档控制器:

self.docInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:myDocumentPath]];

this creates a UIDocumentInteractionController using the interactionControllerWithURL method, which needs an NSURL variable. 这将使用interactionControllerWithURL方法创建一个UIDocumentInteractionController ,该方法需要一个NSURL变量。 We create that NSURL variable using the fileURLWithPath method, passing it our myDocumentPath . 我们使用fileURLWithPath方法创建该NSURL变量,并将其传递给我们的myDocumentPath

The next line calls presentOpenInMenuFromRect Which takes a rectangle and a viewport: 下一行调用presentOpenInMenuFromRect它带有一个矩形和一个视口:

[self.docInteractionController presentOpenInMenuFromRect:CGRectMake(0,0,1,1) inView:UIApplication.sharedApplication.keyWindow animated:YES];

the rectangle is created at position 0,0 with a width and height of 0,0 ( GCRectMake(0,0,1,1) ) and the viewport is taken from the PhoneGap application: UIApplication.sharedApplication.keyWindow 在位置0,0处创建的矩形,宽度和高度为0,0( GCRectMake(0,0,1,1) ),并且视口从PhoneGap应用程序获取: UIApplication.sharedApplication.keyWindow

I'm reeling with joy and amazement that this only took two lines of code to fix. 我很高兴和惊奇地发现,这只需要修复两行代码。 Granted, there's not a way to do it with JavaScript only, but it's still pretty elegant, and works great. 当然,没有办法仅使用JavaScript做到这一点,但它仍然非常优雅,并且效果很好。

You can open your PDF using PhoneGap InAppBrowser( http://docs.phonegap.com/en/2.5.0/cordova_inappbrowser_inappbrowser.md.html#InAppBrowser ) if the PDF is opend from the server using a URL or if its from local device, you can use the PDFViewer plugin: https://github.com/phonegap/phonegap-plugins/tree/master/iOS/PDFViewer . 如果使用URL从服务器打开了PDF,或者从本地设备打开了PDF,则可以使用PhoneGap InAppBrowser( http://docs.phonegap.com/en/2.5.0/cordova_inappbrowser_inappbrowser.md.html#InAppBrowser )打开PDF。 ,您可以使用PDFViewer插件: https : //github.com/phonegap/phonegap-plugins/tree/master/iOS/PDFViewer

Bothways the native default PDF reader will be used to open the PDF. 两种方式都将使用本机默认的PDF阅读器来打开PDF。

I really searched for an ready-to-use solution and gave up at one point. 我真的在寻找一种现成的解决方案,并一度放弃。 There are a few old repositories on GitHub and some articles building on the two lines of the green marked answer - but nothing works satisfying for me. GitHub上有一些旧的存储库,还有一些文章以绿色标记的答案的两行为基础-但没有什么令我满意。

That's why i forked the cordova-plugin-file-opener2 to change the iOS behavior and this works perfect for me. 这就是为什么我叉了cordova-plugin-file-opener2来更改iOS行为的原因,这对我来说很完美。 To allow also parallel usage of the forked plugin, i've renamed it creatively to cordova-plugin-file-opener3 . 为了也允许并行使用分支的插件,我已经创造性地将其重命名为cordova-plugin-file-opener3 It provides the same features as version 2.0.1 of the forked plugin except that on iOS the "Open In"-Dialog is shown. 它提供了与分叉插件的版本2.0.1相同的功能,但在iOS上显示了“打开于”对话框。

The change to the plugin was trivial. 对插件的更改微不足道。 Just replaced one method call and did some renaming. 只是替换了一个方法调用并做了一些重命名。 Don't know why it was so hard to find a working solution - but maybe that i didn't search the right way. 不知道为什么很难找到可行的解决方案-但也许我没有找到正确的方法。

You can find the repository here: 您可以在此处找到存储库:

https://github.com/napolitano/cordova-plugin-file-opener3

Feel free to use it. 随意使用它。

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

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