Opening a remote PDF file

My head is well and truly fried!
I’ve tried all sorts to try and get my pdf’s (which are on my googledrive on the web) to open up on an android device (am testing using my wife’s HTC Desire.)

I can’t make it happen. The following have not worked.

system.openURL ----> just downloads the pdf but doesn’t display it
native.newWebView -----> the pop up is just blank and then can’t get back to the app.
native.showWebPopup ----> the pop up is blank, but back button takes me back to app.
network.request ------> didn’t seem to work, but this is where my head was fried.

Has anyone managed to download AND disply pdf’s (on android in particular)??

Any help would be greatly appreciated. Thanks. [import]uid: 194387 topic_id: 35318 reply_id: 335318[/import]

Android doesn’t come with a PDF viewer. You would have to download a PDF Reader app in order to display PDF files, such as Adobe’s here…
https://play.google.com/store/apps/details?id=com.adobe.reader

Once a PDF Reader/Viewer app is installed, calling [lua]system.openURL()[/lua] will then launch the 3rd party PDF app to view the file. But, you can’t depend on your end-users having an app installed. So, my recommendation is to not use PDFs with your app since Android has no built-in support.
[import]uid: 32256 topic_id: 35318 reply_id: 140395[/import]

Thanks Joshua.

The situation is that I’ve got a PDF viewer installed on the HTC but still, when I call system.openURL() it still only downloads the file. It does not even attempt to open it with the installed pdf reader.
Even testing on my friend’s Samsung Galaxy S3 does the same. There doesn’t seem to be an elegant way of easily opening a pdf on an android device at the moment.

In which case, I’m thinking either:
a) inform the users in the app that they will simply have to go to their downloads notifications and open the pdf manually.
b) convert the pdf’s to html and store them in html format on my google drive, then load them using native.showWebPopup()

Anyone know if webPopups scroll for long content?

Another nice way might be to offer them a choice to view as html or download as pdf.

Has anyone else tackled this problem in a reliable way?
[import]uid: 194387 topic_id: 35318 reply_id: 140402[/import]

I just tried opening a PDF file from my app’s “system.ResourceDirectory” and was able to successfully display it on the following devices:

  • Samsung Galaxy SII (Android 2.3)
  • Nook Color (Android 2.2)
  • Samsung Galaxy Nexus (Android 4.0.4)

The trick is to open the PDF as follows…
[lua]system.openURL(system.pathForFile(“MyDocument.pdf”))[/lua]

That is, you’ll need to call [lua]system.pathForFile()[/lua] on your PDF file when feeding it to the [lua]system.openURL()[/lua] function.

I also have to retract the statement I made up above. Most Android devices do in fact come with a PDF viewer. Although, I’ve found different viewers on different devices. So, it still may be possible that some Android device manufacturers may not include a PDF viewer.

So, to help your code recognize if the device supports displaying the given file, we’ve modified our Android code today to have [lua]system.openURL()[/lua] return false if the OS is unable to open the given URL. This also matches the iOS/Mac behavior that was implemented last December. This code change will be made available in daily build #1016, tomorrow.

This means your can write your code like this…
[lua]local wasOpened = system.openURL(system.pathForFile(“MyDocument.pdf”))
if wasOpened == false then
– Display a message to the user to install a PDF viewer app.
end[/lua]

I hope this helps! [import]uid: 32256 topic_id: 35318 reply_id: 140414[/import]

That helps a lot Joshua. Thanks!

It makes sense to do it that way, i.e. call pathForFile() so that the pdf gets ‘unzipped’ by the corona sdk (as per one of your previous posts.)

I can do it this way for my current app as I only have about 10 pdfs (its an info app) so I can bundle them into the app itself without making the filesize too big.

The icing on the cake however would be to be able to ‘download and open’ as this would mean the app could stay light and users only download the pdfs they need.

Is there an elegant way to use system.url() to do that?

I guess the pertinent point here is that how does one get a handle to, and subsequently open, files that are in the downloads folder of the android device?
system.url(“http://googledrive/…/mydoc.pdf”) gets the file downloaded. Then how to open it?

[import]uid: 194387 topic_id: 35318 reply_id: 140456[/import]

In that case, I recommend that you download your PDF files to [lua]system.DocumentsDirectory[/lua] and then open the PDF file as follows…
[lua]local wasOpened = system.openURL(system.pathForFile(“MyDocument.pdf”, system.DocumentsDirectory))
if wasOpened == false then
– Display a message to the user to install a PDF viewer app.
end[/lua]

Note that the [lua]system.pathForFile()[/lua] function accepts a 2nd argument where you can specify the directory. If you do not provide that 2nd argument, then that function defaults to the [lua]system.ResourceDirectory[/lua].
http://docs.coronalabs.com/api/library/system/pathForFile.html

You can use our [lua]network.download()[/lua] API to download your PDF files.
http://docs.coronalabs.com/api/library/network/download.html

I hope this helps! [import]uid: 32256 topic_id: 35318 reply_id: 140667[/import]

Android doesn’t come with a PDF viewer. You would have to download a PDF Reader app in order to display PDF files, such as Adobe’s here…
https://play.google.com/store/apps/details?id=com.adobe.reader

Once a PDF Reader/Viewer app is installed, calling [lua]system.openURL()[/lua] will then launch the 3rd party PDF app to view the file. But, you can’t depend on your end-users having an app installed. So, my recommendation is to not use PDFs with your app since Android has no built-in support.
[import]uid: 32256 topic_id: 35318 reply_id: 140395[/import]

Thanks Joshua.

The situation is that I’ve got a PDF viewer installed on the HTC but still, when I call system.openURL() it still only downloads the file. It does not even attempt to open it with the installed pdf reader.
Even testing on my friend’s Samsung Galaxy S3 does the same. There doesn’t seem to be an elegant way of easily opening a pdf on an android device at the moment.

In which case, I’m thinking either:
a) inform the users in the app that they will simply have to go to their downloads notifications and open the pdf manually.
b) convert the pdf’s to html and store them in html format on my google drive, then load them using native.showWebPopup()

Anyone know if webPopups scroll for long content?

Another nice way might be to offer them a choice to view as html or download as pdf.

Has anyone else tackled this problem in a reliable way?
[import]uid: 194387 topic_id: 35318 reply_id: 140402[/import]

I just tried opening a PDF file from my app’s “system.ResourceDirectory” and was able to successfully display it on the following devices:

  • Samsung Galaxy SII (Android 2.3)
  • Nook Color (Android 2.2)
  • Samsung Galaxy Nexus (Android 4.0.4)

The trick is to open the PDF as follows…
[lua]system.openURL(system.pathForFile(“MyDocument.pdf”))[/lua]

That is, you’ll need to call [lua]system.pathForFile()[/lua] on your PDF file when feeding it to the [lua]system.openURL()[/lua] function.

I also have to retract the statement I made up above. Most Android devices do in fact come with a PDF viewer. Although, I’ve found different viewers on different devices. So, it still may be possible that some Android device manufacturers may not include a PDF viewer.

So, to help your code recognize if the device supports displaying the given file, we’ve modified our Android code today to have [lua]system.openURL()[/lua] return false if the OS is unable to open the given URL. This also matches the iOS/Mac behavior that was implemented last December. This code change will be made available in daily build #1016, tomorrow.

This means your can write your code like this…
[lua]local wasOpened = system.openURL(system.pathForFile(“MyDocument.pdf”))
if wasOpened == false then
– Display a message to the user to install a PDF viewer app.
end[/lua]

I hope this helps! [import]uid: 32256 topic_id: 35318 reply_id: 140414[/import]

That helps a lot Joshua. Thanks!

It makes sense to do it that way, i.e. call pathForFile() so that the pdf gets ‘unzipped’ by the corona sdk (as per one of your previous posts.)

I can do it this way for my current app as I only have about 10 pdfs (its an info app) so I can bundle them into the app itself without making the filesize too big.

The icing on the cake however would be to be able to ‘download and open’ as this would mean the app could stay light and users only download the pdfs they need.

Is there an elegant way to use system.url() to do that?

I guess the pertinent point here is that how does one get a handle to, and subsequently open, files that are in the downloads folder of the android device?
system.url(“http://googledrive/…/mydoc.pdf”) gets the file downloaded. Then how to open it?

[import]uid: 194387 topic_id: 35318 reply_id: 140456[/import]

In that case, I recommend that you download your PDF files to [lua]system.DocumentsDirectory[/lua] and then open the PDF file as follows…
[lua]local wasOpened = system.openURL(system.pathForFile(“MyDocument.pdf”, system.DocumentsDirectory))
if wasOpened == false then
– Display a message to the user to install a PDF viewer app.
end[/lua]

Note that the [lua]system.pathForFile()[/lua] function accepts a 2nd argument where you can specify the directory. If you do not provide that 2nd argument, then that function defaults to the [lua]system.ResourceDirectory[/lua].
http://docs.coronalabs.com/api/library/system/pathForFile.html

You can use our [lua]network.download()[/lua] API to download your PDF files.
http://docs.coronalabs.com/api/library/network/download.html

I hope this helps! [import]uid: 32256 topic_id: 35318 reply_id: 140667[/import]

I can’t seem to open ANY fie with this method. I need to open a PDF stored in the resource directory.

Thinking it was an issue with PDFs in particular, I even tried a JPG:

    system.openURL(system.pathForFile(“background.jpg”))

Nothing happens on the device. No error reported on the Xcode console, nothing.

I have used debug code to confirm that the file exists on the device and that the code is executing that line. It does, but nothing happens. I can open a remote file on my server, but I cannot seem to open any file in the Resources directory.

iPhone 5

Build: 2013.2100

The method I posted above will work on Android.

For iOS, I believe a native.newWebView() can display a PDF file.  I’m not an iOS developer here, so I don’t for sure, but I’ve heard some people having success with this.

For Pro subscribers, you can use the quicklook plugin (http://docs.coronalabs.com/daily/plugin/CoronaProvider_native_popup_quickLook/) for IOS to view PDF files.

Rob

I can’t seem to open ANY fie with this method. I need to open a PDF stored in the resource directory.

Thinking it was an issue with PDFs in particular, I even tried a JPG:

    system.openURL(system.pathForFile(“background.jpg”))

Nothing happens on the device. No error reported on the Xcode console, nothing.

I have used debug code to confirm that the file exists on the device and that the code is executing that line. It does, but nothing happens. I can open a remote file on my server, but I cannot seem to open any file in the Resources directory.

iPhone 5

Build: 2013.2100

The method I posted above will work on Android.

For iOS, I believe a native.newWebView() can display a PDF file.  I’m not an iOS developer here, so I don’t for sure, but I’ve heard some people having success with this.

For Pro subscribers, you can use the quicklook plugin (http://docs.coronalabs.com/daily/plugin/CoronaProvider_native_popup_quickLook/) for IOS to view PDF files.

Rob