Monday, May 14, 2012

Displaying PDF files

I recently had a request by a client to be able to display links that would go to hosted PDF files (files that are out in the cloud somewhere and not resident to the device).  Now there is no guarantee that a particular device has a PDF viewer, say from Adobe installed. And using that viewer would kick the person out of the app anyway when the intent fires.  One easy solution for this is to pass the PDF link to Google documents which would automatically convert the PDF to html and display it.

if (url !=null && url.endsWith(".pdf")) {
Uri uri = Uri.parse("http://docs.google.com/viewer?url=" + url);
Intent intent = new Intent(Intent.ACTION_VIEW);
 intent.setDataAndType(uri, "text/html");
startActivity(intent);
}