25

I want to display pdf contents on webview.

Here is my code:

WebView webview = new WebView(this); 
setContentView(webview); 
webview.getSettings().setJavaScriptEnabled(true); 
webview.loadUrl("URL/Demo_PDF.pdf");

Problem:

When i am trying to run the application, at that time I am getting blank screen.

And also, if there is any PDF viewer then also suggest me !!

FYI, I have already set internet permission.

Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
  • 2
    For all, **This question is too old now, at that time PDF Viewer/Reader were not available so i did with this solution** – Paresh Mayani Jun 22 '12 at 13:53

4 Answers4

50

Finally, i got a solution, actually i made a trick to load a pdf file using Google Docs inside a webview:

webview.loadUrl("http://docs.google.com/gview?embedded=true&url=http://myurl.com/demo.pdf");
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
  • In my case it fails because "Your browser must support Javascript" although I enable Javascript in my Android browser. – anticafe Mar 14 '11 at 07:36
  • 4
    @anticafe have you used setJAvascriptenabled(true) inside code ? – Paresh Mayani Mar 14 '11 at 08:43
  • 1
    oh I see. By default Android webview disables Javascript. So every times create a webview, we must setJAvascriptenabled(true). – anticafe Apr 03 '11 at 02:18
  • @anticafe ya it is, you have to. – Paresh Mayani Apr 04 '11 at 05:27
  • @Paresh "Disadvantage:network connection". Anyhow the pdf is also located on a server, so network connection is a must even if you were not reading the pdf using a webview.. so this way i can use google gview to load pdf from web into a webview.. what if my pdf is on the sdcard.? any sugesstions/solutions. – Varun Jun 01 '11 at 09:28
  • @paresh : i am getting error : Sorry, we are unable to retrieve the document for viewing or you don't have permission to view the document. I have event opened the Url loaded in the my Desktop browser. Do we need some authentication? I have also enabled javascript =true from code... – Rakesh Gondaliya Sep 02 '11 at 10:28
  • 1
    Why do you all post the same solution for this quetsion? This is NOT a solution for PDF offline reading. – emeraldhieu Apr 26 '12 at 06:57
  • 1
    @Emerald214 pls check the question date, at that time muPDF and other kind of API and library were not available. – Paresh Mayani Apr 26 '12 at 07:02
  • @PareshMayani: when i use this url it redirect me to viewer term and condition page rather than opening PDF. Why? – Tofeeq Ahmad Jan 24 '13 at 06:15
  • @PareshMayani: Is there any robust solution to display pdf from sdcard in application itself ? – Mehul Joisar Mar 26 '14 at 11:08
  • @MehulJoisar As I have mentioned in comment over the question, I had used this solution as there weren't any PDF library/framework available, but now there are many libraries/tools available (e.g. muPDF) by which you can view/read/edit PDF files into the android application. Give `muPDF` at least a try! – Paresh Mayani Mar 26 '14 at 11:24
  • @PareshMayani: Yes, I am working on it on the other hand but I was looking for optional way because muPDF requires to build by NDK. – Mehul Joisar Mar 26 '14 at 11:33
  • 1
    @MehulJoisar so what! Ultimately you are getting PDF reader in free :) – Paresh Mayani Mar 26 '14 at 12:31
  • @PareshMayani: Yeah! that's true :) – Mehul Joisar Mar 26 '14 at 12:35
10

I'm sorry but WebView does not display PDF content.
What you might want is a PDF viewer that responds to the PDF MIME type.

Here are a couple of free open-source libraries you might want to check:

systempuntoout
  • 71,966
  • 47
  • 171
  • 241
1

WebView does not have a PDF plugin. You should store the PDF locally and open an intent for viewing that kind of content.

bluish
  • 26,356
  • 27
  • 122
  • 180
rds
  • 26,253
  • 19
  • 107
  • 134
1

You can try using mupdf (www.mupdf.com) You can build a native code library (libmupdf.so) to be loaded onto your android project using the ndk. This would ensure a lot faster rendering of pdf files... check out the project at : http://mupdf.com/repos/mupdf/android/

Roy Samuel
  • 790
  • 9
  • 24
  • I was successful in building the library. (libmupdf.so) – Roy Samuel Jan 21 '11 at 11:20
  • i havent idea of NDK yet, so it is difficult me to implement such, can you pass the builded library to me?? – Paresh Mayani Jan 21 '11 at 13:02
  • As of now, the library that I have built generates png files for all the pages in a pdf & aslo stores the raw raster data onto the sdcard. As of rendering the pdf, as is onto a 'View' on Android, you'd need to refer the 'mupdf-android' project. I can share the library I've built, if required. – Roy Samuel Feb 02 '11 at 06:09
  • Roy, can you pass along the "libmupdf.so" library you built plz? sachin6670i@gmail.com – Samuh Feb 24 '11 at 13:27
  • Sorry for the delay, It's quite a huge size, come's up to 7 MB. Besides, the interface I have developed is only used to export raster data... Let me know if I can be of help programmatically... – Roy Samuel Mar 18 '11 at 04:53