Open PDF File on Android

You must Login before you can answer or comment on any questions.

Has anyone found a way to open or view a PDF file on Android? I understand that webview doesn't support PDF on android. I have PDF viewers on the handset, but how can I open the PDF from the app?

8 Answers

Accepted Answer

With some of the new APIs that have been exposed, this is now a lot easier. You don't need to hack anything in to the source code. Take a look at the following:

try {
    var f = Ti.Filesystem.getFile('your.pdf');
    Ti.Android.currentActivity.startActivity(Ti.Android.createIntent({
        action: Ti.Android.ACTION_VIEW,
        type: 'application/pdf',
        data: f.getNativePath()
    }));
}
catch (err) {
    var alertDialog = Titanium.UI.createAlertDialog({
        title: 'No PDF Viewer',
        message: 'We tried to open a PDF but failed. Do you want to search the marketplace for a PDF viewer?',
        buttonNames: ['Yes','No'],
        cancel: 1
    });
    alertDialog.show();
    alertDialog.addEventListener('click', function(evt) {
        if (evt.index == 0) {
            Ti.Platform.openURL('http://search?q=pdf');
        }
    });
}

— answered 3 years ago by Dawson Toth
answer permalink
12 Comments
  • Awesome. Thanks man. I was not looking forward to hacking the source each time a new SDK came out.

    — commented 3 years ago by Ken Rucker

  • Hi,

    I tried the same code, but for some reason, am getting 'Invalid file path' error. This is the path used . I am downloading a pdf file to the same path before invoking this below code.

    var f = Ti.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,'my.pdf');
    Ti.Android.currentActivity.startActivity(Ti.Android.createIntent({
                    action: Ti.Android.ACTION_VIEW,
                    type: 'application/pdf',
                    data: f.getNativePath()
                }));
    The device I'm using is a Spring Evo 4G which I believe is running Android 2.3.

    — commented 3 years ago by Prathima Doraiswamy

  • @Prathima: You are getting the invalid path error because the PDF reading app that your intent is launching can't access the file. Your application's data directory is sandboxed to your app, so no one else can access it. You need to either a) place the PDF in a publicly accessible place, like on the SDCard, or b) set the file as world_readable. Note that option b isn't possible with Titanium yet, so you would need to create your own module.

    There is an open feature request for expose the world_readable mode: http://jira.appcelerator.org/browse/TIMOB-2957

    — commented 3 years ago by Dawson Toth

  • Show 9 more comments

Actually, I have a better answer now.

If you look at the source for what Titanium.Platform.openURL is doing on Android, you'll see it's creating and starting an intent. This works great for PDFs on most Android devices.

But it doesn't work exactly like it should, and I found it didn't work at all on a bunch of HTC android devices.

I got around this by pulling the source for Titanium Mobile down, and adding a new method to the Titanium.Platform: "openURLWithType". It looks something like this:

public boolean openURLWithType(String url, String type) {
               if (DBG) {
                       Log.d(LCAT, "Launching viewer for: " + url);
               }
               Uri uri = Uri.parse(url);
               Intent intent = new Intent(Intent.ACTION_VIEW, uri);
               intent.setDataAndType(uri, type);
               intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
               try {
                       getTiContext().getActivity().startActivity(intent);
                       return true;
               } catch (ActivityNotFoundException e) {
                       Log.e(LCAT,"Activity not found: " + url, e);
               }
               return false;
}
Two small differences: 1) intent.setDataAndType(uri, type) will let you specify what you're linking to so that third party apps can pick it up. 2) intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)

Then I can do the following and the PDF is properly launched:

if (Ti.Platform.openURLWithType('my.pdf', 'application/pdf')) {
   // success!
}
else {
   alert('You need to download a PDF viewer from the market!');
}
I'm working with the latest code from the 1.4x branch on github. I remember hearing rumor that they were adding support for this in 1.5 or something, but I don't know anything definite.

DM me @dawsontoth on Twitter if you want to know more about this approach...

— answered 3 years ago by Dawson Toth
answer permalink
4 Comments
  • Any chance you could give me some instruction on hour to do this? I guess the TI code is compiled or something on my dev Mac. I don't see where the raw code is to where I can modify it to add this function... or how I would recomoile TI or whatever after the new function is added. Kind of a newbie to advanced things like this. Maybe there is a module somewhere I could use instead? Any help is greatly appreciated. Open PDF with open URL doesn't work on my HTC or may Acer tablet.

    — commented 3 years ago by Ken Rucker

  • I think this may be what I need. Haven't tried yet but maybe it will help others-

    http://www.metalsoft.com.ar/2011/07/22/titanium-mobile-android-open-urls-specifying-the-type/

    — commented 3 years ago by Ken Rucker

  • Got it going with the link above. Works awesome. Thanks Dawson and Sebastian. Real life savers.

    — commented 3 years ago by Ken Rucker

  • Show 1 more comment

you can simply open it using Google viewer http://docs.google.com/viewer?pli=1

I'm new to android and I'm trying to develop an application which creates the ".pdf" files i.e. i have one form(contains the information of the person) this information should be stored in the .pdf format.. so how to do this ???please help me out in this...

— answered 2 years ago by varun k
answer permalink
1 Comment
  • Since I dont know anything about generating PDFs locally, i would use a server for doing that. Send your data via http, to a server which generates the PDF with your data, and send that back to you (your phone)

    — commented 2 years ago by Lars C. Magnusson

I was able to load a PDF inside the android browser using Pdf.js https://github.com/mozilla/pdf.js

It's a javascript library the renders it into the HTML 5 Canvas element. It's a little heavy. That's the only downside. It renders out near perfect though. Also, I was able to get it to work on Android 4.0+ in the emulator. I tried it on my android device which is 2.2.2 and it didn't work.

— answered 2 years ago by Ronnie Swietek
answer permalink
1 Comment
  • How you do that, I tried withe thier examples and there was a lot of problem. Pdf doesn't appeared :/ Could you give me tip?

    — commented 2 years ago by Tomasz Mucha

i was wondering whether this vb.net pdf viewer can integrate with the android system. if so, then everything will become much easier. you can load and and process pdf documents.

Your Answer

Think you can help? Login to answer this question!