8

I have a strange problem with my iPad App in Phone Gap. The problem is that I have to open PDF document in my app through links and when I click the link which opens the PDF, it shows me the PDF document with no back link.

Hence, when I open the PDF document in my app through a link, it takes me to a dead end and there is no way I can go back to the main page of my app.

My question is that how can I have a Top-Bar, when I open a PDF which could take me back to my home page? Any internal element for the iPad may be?

Thanks a lot.

Steve
  • 2,546
  • 8
  • 49
  • 94

5 Answers5

12

Try using the In App Browser plugin.

If you're using a later Phonegap / Cordova version (2.8.0, 2.9.0 etc) it should come with it - nothing else to install.

http://docs.phonegap.com/en/2.9.0/cordova_inappbrowser_inappbrowser.md.html#InAppBrowser

It will allow you to open the PDF in the a new 'window' that overlays your app. It has a 'Done' button that users can use to close it and return to your app when they are finished.

You would open the PDF using the In-App Browser, using something like this:

window.open('http://whitelisted-url.com/pdftoopen.pdf', '_blank');

I.e. the _blank option triggers the In-App Browser plugin. If you were to use _system instead it might open it in iBooks (just guessing there - not 100% sure if it would use iBooks).

asgeo1
  • 9,028
  • 6
  • 63
  • 85
  • Does someone happend to know why this is not working for me also? I use an iphone 4 and a cordova 3. – paulalexandru Aug 14 '14 at 07:41
  • i dont get the "it should come with it". im using cordova 3.6 and yes you do have install `org.apache.cordova.inappbrowswer` for it to work. This worked great. Thanks! – allwynmasc Oct 18 '14 at 09:42
  • @Gallwynmasc - For Cordova version 2.x, many of the plugins (including In App Browser) were included by default. With 3.x, the plugins are all optional, and you install the ones you want. – asgeo1 Oct 20 '14 at 00:22
5

Try prefixing https://docs.google.com/viewer?url= in the URL

like, window.open('https://docs.google.com/viewer?url=http://www.example.com/example.pdf&embedded=true', '_blank', 'location=yes');

ngrashia
  • 9,869
  • 5
  • 43
  • 58
BetRob
  • 251
  • 1
  • 7
  • 12
  • This requires the user to sign in to Google Drive. Also make sure to encode the url parameter. Clever solution though. – Keith Apr 09 '14 at 16:09
  • I uploaded the PDF to Google Drive and set sharing to public, then shared the link. I don't believe this method requires the user to be logged in. – Bruno Jun 03 '14 at 15:16
1

Try this to open any kind of documents from URL using following steps:

It works for me both on Android and IOS. I used it for open images and PDF files.

Android : It opens files using system apps if available, otherwise it give an error, which you can handle.

IOS : It opens files in popup like view with Done button and Option button.

It doesn't show your docs URL.

Source is available here : https://github.com/ti8m/DocumentHandler

SANAT
  • 8,489
  • 55
  • 66
0

Thanks asgeo1,

I solved it by using window.open().

<a href="#" onclick="window.open('http://12example.pdf', '_blank', 'location=no');"><img src="images/samplens.jpg" border="0" /></a>

Hope it helps.

Steve
  • 2,546
  • 8
  • 49
  • 94
0

I've ended up using WebIntent

as described here. The tricky part was to modify WebIntent.java to properly identify file type:

String type = obj.has("type") ? obj.getString("type") : null;  
 // New code starts  
 Uri uri = obj.has("url") ? Uri.parse(obj.getString("url")) : null;  
 String extension = MimeTypeMap.getFileExtensionFromUrl(obj.getString("url"));  
 if(extension != null){  
      MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();  
      type = mimeTypeMap.getMimeTypeFromExtension(extension);  
 }  
 // New code ends  
 JSONObject extras = obj.has("extras") ? obj.getJSONObject("extras") : null;
lucas
  • 43
  • 4