I don’t know about you, but I have had multiple clients request a PDF viewer in their app. Personally, I am not a huge fan of PDF’s but they serve their purpose. Below are 3 examples of ways to add a PDF viewer to your Titanium Mobile application.
The easy way
Did you know Google Apps has a fantastic little service for viewing PDF files? As you might have guessed, based on the title of this blog post, it also allows you to view a PDF. It does not seem to work in the Ti.UI.webView. However, using openUrl seems to work quite well.
Android Intents for local files
If you would like to use Android intents for viewing local PDF files saved on the device, Tony Lukasavage has a great blog post about sharing information between intents. Remember that if you want to display a remote pdf file you will need to download and save it first to the external storage device before you can invoke the intent to view it.
(Sharing project assets with Android intents)[http://www.appcelerator.com/blog/2011/09/sharing-project-assets-with-android-intents/]
Paid Module
With this one, “there are a few, uh, provisos. Ah, a couple of quid pro quos.” One, I have never used this module and can’t guarantee it will work. There is only one 5 star review so take it with a grain of salt. Two, it cost $15.99 per year. With those two items out of the way, have at it. That said, it is the only way I have seen to view a PDF within your app.
As you can see PDF’s on Android are a little tricky. If you have any thoughts on how to add a PDF viewer or if there is a method or module I am missing please post it in the comments below so that I can add it.
Comments
Michael
Another (free) way is to use pdf.js (http://mozilla.github.io/pdf.js/) and a webview. Then you have almost the same look and feel as on iOS. It took a while to open the file on my Nexus 7 (2012) but it displayed the test pdfs without any problem
Josh Jensen Post author
Thanks for the note Michael. I will check that out.
Ray Belisle
Josh,
Do you know why the viewer won’t load in a webview? It would be nice to have control (ie. maybe other controls) displayed along with the view.
Josh Jensen Post author
Hi Ray, I have not dug into it enough to know what the issue is. I have not tried it myself but as Michael said above you might want to give pdf.js a try. (http://mozilla.github.io/pdf.js/)
pragnesh
hi
how to used pdf.js? in Titanium example provide?
pragnesh
support kitkat
PdfDocument document = new PdfDocument();
// crate a page description
PageInfo pageInfo = new PageInfo.Builder(new Rect(0, 0, 100, 100), 1).create();
// start a page
Page page = document.startPage(pageInfo);
// draw something on the page
View content = getContentView();
content.draw(page.getCanvas());
// finish the page
document.finishPage(page);
http://developer.android.com/reference/android/graphics/pdf/PdfDocument.html
Michael
this will create a pdf from a view as far as i understood. This article is about reading a pdf on android
Michael
You can use the example from the pdf.js page and just load this in a webview. If you want to keep it modular you can change the filename inside the html file to PDFFILE, load the file with getFile, replace PDFFILE with your filename and then pass this string to the webview. So you have one universal HTML page layout and can open different PDFs with it. pdf.js takes a bit to load on some devices but at least it works :)
Michael
Here is an example on how to use pdf.js on Android: https://github.com/m1ga/ti-pdf-js
pragnesh
Michael
Nice Post
Thanks for example
Mehmet Akif AKTAŞ
Thank you. I like easy way :)
Ishan Singh
Hi All,
You can use this open source module build by me.
https://github.com/ishansingh2003/Android-Pdf-Viewer
Though its ui its not good but its working. At least you will have an idea. This will open pdf inside your app only. Pleas help to make it more better.
Rahul Mishra
Hi Michel
I am using your link (http://mozilla.github.io/pdf.js/).This link working fine on web browser but i am using on ionic platform this is not working.i am not finding any error on cosole and i am using alert inside this method (PDFJS.getDocument(url).then(function getPdfHelloWorld(pdf) {alert(“hello”); })
but i could not got any alert solution if you have any solution so please send me…
chithra
hi everyone….
i dnt knw how to import pdf viewer in andriod studio..i want hyperlink to open pdf form for next page…pls help me