How To Open And Show Pdf File From Url Using Native Webview

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

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

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.

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

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.

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.

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.

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

Warren

Also note the quicklook plugin for iOS:

https://coronalabs.com/blog/2013/12/24/tutorial-introducing-the-quicklook-plugin-ios/

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

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

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.

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

Warren

Also note the quicklook plugin for iOS:

https://coronalabs.com/blog/2013/12/24/tutorial-introducing-the-quicklook-plugin-ios/

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

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

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.