Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

How To Open And Show Pdf File From Url Using Native Webview
Started by springmorning Apr 09 2013 06:42 AM

8 replies to this topic
android webview pdf

Best Answer springmorning , 10 April 2013 - 12:30 AM

Hi Brent,

 

Thanks for your reply! 

My goal was to show a PDF when a user clicks on its hyperlink from inside the native webview (in Android). I tried your suggestion (Simple PDF Reader) but I found it did not work straight out of the box. At least not on Android.

 

So I changed my approach and hooked the "load" event from the webview. Everytime the webview wants to open a URL that ends with ".pdf" I now reroute that URL to Google Drive (using https://docs.google.com/viewer and system.openURL). The result is that the PDF is displayed in the device's own web browser. From within this view, the user can also download the PDF to the device.

[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

springmorning

[GLOBAL: userInfoPane.html]
springmorning
  • Contributor

  • 129 posts
  • Corona SDK

I have developed a small app that displays (among others) a mobile website using the native webview. On iOS it all works and looks fantastically.

On Android (tested with v4.2.2, Nexus 7 and Galaxy Nexus) it won't do anything with hyperlinks to external PDF files. It doesn't download it, or display it to the user. Nothing happens. On iOS, the PDF is shown directly.

 

My ultimate goal is to be able to open and show the PDF to the user directly. But I can also live with the fact that the PDF gets downloaded when the user clicks on the hyperlink (which is the case when the website is opened on the device using the webbrowser).

 

Is this possible? I tried installing a PDF viewing app, but that did not solve it.

 

I'd appreciate any response.

 

Best regards,

~Rob

 

 



[TOPIC: post.html]
#2

bjsorrentino

[GLOBAL: userInfoPane.html]
bjsorrentino
  • Veteran

  • 8,506 posts
  • Corona SDK

Hi Rob,

This might be a solution to investigate:

https://github.com/boostup/Corona-SDK-Simple-PDF-Reader

 

I can't vouch for everything it does, or even if this is exactly what you need, but it's been used by others in the community with (as far as I know) good results.

 

Best regards,

Brent



[TOPIC: post.html]
#3

springmorning

[GLOBAL: userInfoPane.html]
springmorning
  • Contributor

  • 129 posts
  • Corona SDK

  Best Answer

Hi Brent,

 

Thanks for your reply! 

My goal was to show a PDF when a user clicks on its hyperlink from inside the native webview (in Android). I tried your suggestion (Simple PDF Reader) but I found it did not work straight out of the box. At least not on Android.

 

So I changed my approach and hooked the "load" event from the webview. Everytime the webview wants to open a URL that ends with ".pdf" I now reroute that URL to Google Drive (using https://docs.google.com/viewer and system.openURL). The result is that the PDF is displayed in the device's own web browser. From within this view, the user can also download the PDF to the device.



[TOPIC: post.html]
#4

henrik5

[GLOBAL: userInfoPane.html]
henrik5
  • Contributor

  • 239 posts
  • Corona SDK

springmorning's answer works great, also in webPopups!

 

Note that if you use it with openURL in the simulator, you may be fooled into thinking the app user needs a Google account login. This is because your browser has some cookie from google.com, and in the app you don't need it.

 

Note also that using Google Viewer is better than uploading PDFs to Google Drive and share them from there. Shared google docs show some extra stuff while loading (at least on Android) and the controls become so small that you can't tap them!

 

This is the best way I've found, and it's worth using Google Viewer with online documents even over bundling PDFs with the app.



[TOPIC: post.html]
#5

WarrenW

[GLOBAL: userInfoPane.html]
WarrenW
  • Contributor

  • 830 posts
  • Corona SDK

Does this work with the newWebView within the app? Or only with openURL using an external browser?

 

Warren



[TOPIC: post.html]
#6

corona273

[GLOBAL: userInfoPane.html]
corona273
  • Contributor

  • 482 posts
  • Enterprise

[TOPIC: post.html]
#7

henrik5

[GLOBAL: userInfoPane.html]
henrik5
  • Contributor

  • 239 posts
  • Corona SDK

I'm pretty sure not much more in the way of support has been added to the webView.

Just wanted to say that just after my previous post, Google deprecated Google Viewer, luckily I found a switch/parameter that made Google Drive PDF links usable, strangely "&overridemobile=true".



[TOPIC: post.html]
#8

WarrenW

[GLOBAL: userInfoPane.html]
WarrenW
  • Contributor

  • 830 posts
  • Corona SDK

I was hoping I can still show the PDF within the app and not have to open an external web browser to show it.



[TOPIC: post.html]
#9

henrik5

[GLOBAL: userInfoPane.html]
henrik5
  • Contributor

  • 239 posts
  • Corona SDK

Yes, the switch allows viewing of PDFs on Android in webViews. It can also be used on IOS, but I've used the platform detection that was already in my sources to show PDFs using the IOS webViews' own functionality, basically just because it's slightly smoother.

 

If you mean PDFs included in the app package as files, you could do such a switch for IOS. There's absolutely no way to show such a file on Android inside the app. There's just no PDF viewer in the OS. The file has to be uploaded to Google Drive and converted for viewing there. It does work well though. And allows changes without uploading a new app, just put the URLs in an on-line JSON file (or create a redirect on some fixed address) since the URL will change if you uploaded an updated document.




[topic_controls]
[/topic_controls]