Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Opening a remote PDF file
Started by tabrezk Jan 25 2013 02:59 AM

8 replies to this topic
[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

tabrezk

[GLOBAL: userInfoPane.html]
tabrezk
  • Observer

  • 8 posts
  • Corona SDK

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.
uid: 194387 topic_id: 35318 reply_id: 335318


[TOPIC: post.html]
#2

Joshua Quick

[GLOBAL: userInfoPane.html]
Joshua Quick
  • Moderator

  • 3,397 posts
  • Corona Staff

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
system.openURL()
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.
uid: 32256 topic_id: 35318 reply_id: 140395


[TOPIC: post.html]
#3

tabrezk

[GLOBAL: userInfoPane.html]
tabrezk
  • Observer

  • 8 posts
  • Corona SDK

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?
uid: 194387 topic_id: 35318 reply_id: 140402


[TOPIC: post.html]
#4

Joshua Quick

[GLOBAL: userInfoPane.html]
Joshua Quick
  • Moderator

  • 3,397 posts
  • Corona Staff

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...
system.openURL(system.pathForFile("MyDocument.pdf"))


That is, you'll need to call
system.pathForFile()
on your PDF file when feeding it to the
system.openURL()
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
system.openURL()
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...
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


I hope this helps!
uid: 32256 topic_id: 35318 reply_id: 140414


[TOPIC: post.html]
#5

tabrezk

[GLOBAL: userInfoPane.html]
tabrezk
  • Observer

  • 8 posts
  • Corona SDK

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?

uid: 194387 topic_id: 35318 reply_id: 140456


[TOPIC: post.html]
#6

Joshua Quick

[GLOBAL: userInfoPane.html]
Joshua Quick
  • Moderator

  • 3,397 posts
  • Corona Staff

In that case, I recommend that you download your PDF files to
system.DocumentsDirectory
and then open the PDF file as follows...
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


Note that the
system.pathForFile()
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
system.ResourceDirectory
.
http://docs.coronalabs.com/api/library/system/pathForFile.html

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

I hope this helps!
uid: 32256 topic_id: 35318 reply_id: 140667


[TOPIC: post.html]
#7

jack2014

[GLOBAL: userInfoPane.html]
jack2014
  • Observer

  • 22 posts
  • Corona SDK

 
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


[TOPIC: post.html]
#8

Joshua Quick

[GLOBAL: userInfoPane.html]
Joshua Quick
  • Moderator

  • 3,397 posts
  • Corona Staff

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.



[TOPIC: post.html]
#9

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,622 posts
  • Enterprise

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




[topic_controls]
[/topic_controls]