3

I am having webview in my app, i am loading website in my webview and in that website there are multiple links of PDF file but while i am clicking on that it is not opening in webview, if i am checking it in my windows's browser it opening. i have enabled javascript but no luck, i used following properties also

webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setWebViewClient(new myWebClient());
Rajesh Panchal
  • 1,140
  • 3
  • 20
  • 39
  • android webview doesnot support pdf content, You may use google docviewr to show your pdf file – CoolMonster Apr 02 '14 at 13:40
  • try to implement using thirdparty library like mupdf and all as in android webview doesn't support pdf and google docviewer hv sme limit like after opening pdf for some number of time. it will gve bandwith message where u cnt view pdf even in google doc – user1140237 Apr 02 '14 at 13:52
  • You can find my answer here [http://stackoverflow.com/questions/36979733/load-pdf-file-on-webview-with-okhttp-android](http://stackoverflow.com/questions/36979733/load-pdf-file-on-webview-with-okhttp-android) – Vansi May 02 '16 at 10:04

3 Answers3

3

Android does not work like iOS in this respect. The WebView widget cannot display PDF documents on Android.

You will have to:

  • Use a third party library (unfortunately, most open source pdf libs are GPL)

  • Open a pdf viewer app via an Intent

  • Use Google docs in a webview

FD_
  • 12,947
  • 4
  • 35
  • 62
1

this may help you,

String myPdfUrl = "http://example.com/awesome.pdf";
String url = "http://docs.google.com/gview?embedded=true&url=" + myPdfUrl;
Log.i(TAG, "Opening PDF: " + url);
webView.getSettings().setJavaScriptEnabled(true); 
webView.loadUrl(url);
MdDroidd
  • 76
  • 1
  • 4
1

Whenever you want to open pdf saved in google drive this code can be used

     WebView view=(WebView)findViewById(R.id.webView);
     view.setWebViewClient(new WebViewClient());
     String url=getIntent().toString("value1");
     String URL="https://docs.google.com/viewer?url="+url; 
     view.loadUrl(Url);
  • this opens in google doc , i want to open within webview – Akash Bisariya Oct 19 '16 at 10:33
  • This open the google docs inside your activity. Please define the webview inside your activity. view.setWebViewClient(new WebViewClient()); add this line to code. if still does not work. Please post your code. – Ashish Kapoor Oct 19 '16 at 19:48
  • what do you mean by view.setWebViewClient(new WebViewClient()); Do you know why webviewclient is used in webview. – Akash Bisariya Oct 20 '16 at 06:09
  • When the user clicks a link from a web page in your WebView, the default behavior is for Android to launch an application that handles URLs. Usually, the default web browser opens and loads the destination URL. However, you can override this behavior for your WebView, so links open within your WebView. This is the use of WebViewClient() – Ashish Kapoor Oct 20 '16 at 15:17
  • Let me know it worked for you or not and also rank me if it helped you :) – Ashish Kapoor Oct 20 '16 at 15:18
  • thanks mate :), I just need this -- String URL="https://docs.google.com/viewer?url="+url; It solves my problem. – Akash Bisariya Oct 21 '16 at 06:48