1
votes

The iOS safari browser has a handy option that appears whenever you open a PDF page in the browser

If you have Adobe Reader installed, a button will appear that says:

Open in "Adobe 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:

<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. 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.

Thanks in advance!

3
The browser usually detect the MIME header and then routes it to the application. JavaScript cannot do this. - Diodeus - James MacFarlane
isn't it something like <script>window.location="document.pdf";</script> ? It might open in Adobe Reader, or the browser window, or download depending on user preference. - antonyh
It opens in the browser window, which has the HUD hidden, unfortunately. - strack
could you change the value of the meta to no using javascript before trying to open the document? It might trick it into showing the button; just an idea, I don't even know if it's possible to reset meta values via script. - antonyh

3 Answers

1
votes

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.

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.

So, with my file location as an input (let's call my input variable myDocumentPath), you just need to declare a document controller:

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

this creates a UIDocumentInteractionController using the interactionControllerWithURL method, which needs an NSURL variable. We create that NSURL variable using the fileURLWithPath method, passing it our myDocumentPath.

The next line calls presentOpenInMenuFromRect Which takes a rectangle and a viewport:

[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

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.

0
votes

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.

Bothways the native default PDF reader will be used to open the PDF.

0
votes

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.

That's why i forked the cordova-plugin-file-opener2 to change the iOS behavior and this works perfect for me. To allow also parallel usage of the forked plugin, i've renamed it creatively to 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.

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.