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?

9 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');
        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.addEventListener('click', function(evt) {
        if (evt.index == 0) {

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

    — commented 4 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');
                    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 4 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:

    — commented 4 years ago by Dawson Toth

  • Show 15 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);
               try {
                       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 5 years ago by Dawson Toth
answer permalink
  • 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 4 years ago by Ken Rucker

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

    — commented 4 years ago by Ken Rucker

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

    — commented 4 years ago by Ken Rucker

  • Show 1 more comment

you can simply open it using Google viewer

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 4 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 3 years ago by Lars C. Magnusson

I was able to load a PDF inside the android browser using 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 3 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 3 years ago by Tomasz Mucha


Get the pdf from resourcesDirectory, and trying to write into tempDirectory. After successfully saved into tempDirectory, try to open in the Adobe Reader. Unable to open getting error message like file corrupted. Please help me to write the pdf in proper way.

var pdfFile = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory,'My_PDF.pdf');

        var tmpFile = Ti.Filesystem.getFile(Ti.Filesystem.tempDirectory,'tmpFile.pdf');
        tmpFile.write(;"Temp Directories:" + Ti.Filesystem.tempDirectory);

Thanks in Advance..

Your Answer

Think you can help? Login to answer this question!