How can I display a pdf document in webview?

I want to display pdf content in webview. Here is my code:

WebView webview = new WebView(this); setContentView(webview); webview.getSettings().setJavaScriptEnabled(true); webview.loadUrl("http://www.adobe.com/devnet/acrobat/pdfs/pdf_open_parameters.pdf"); 

I get a blank screen. I also set the internet permission.

+105
android pdf webview
Apr 16 '10 at 20:40
source share
10 answers

You can use the Google Docs Viewer to read your online document:

 WebView webview = (WebView) findViewById(R.id.webview); webview.getSettings().setJavaScriptEnabled(true); String pdf = "http://www.adobe.com/devnet/acrobat/pdfs/pdf_open_parameters.pdf"; webview.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf); 
+157
Mar 14 2018-11-11T00:
source share

If you use only the view URL, the user does not have access to the google account.

 https://docs.google.com/viewer?url=http://my.domain.com/yourPdfUrlHere.pdf 
+32
Apr 19 '12 at 9:23
source share

Opening PDFs with Google Docs is a bad idea in terms of user experience. It is really slow and indifferent.

Solution after API 21

Starting with api 21, we have a PdfRenderer that helps convert PDF to bitmap. I have never used this, but it seems easy enough.

Solution for any level of API

Another solution is to download the PDF file and transfer it using Intent to a special PDF application that will display it. A quick and enjoyable user experience, especially if this feature is not central to your application.

Use this code to download and open PDF

 public class PdfOpenHelper { public static void openPdfFromUrl(final String pdfUrl, final Activity activity){ Observable.fromCallable(new Callable<File>() { @Override public File call() throws Exception { try{ URL url = new URL(pdfUrl); URLConnection connection = url.openConnection(); connection.connect(); // download the file InputStream input = new BufferedInputStream(connection.getInputStream()); File dir = new File(activity.getFilesDir(), "/shared_pdf"); dir.mkdir(); File file = new File(dir, "temp.pdf"); OutputStream output = new FileOutputStream(file); byte data[] = new byte[1024]; long total = 0; int count; while ((count = input.read(data)) != -1) { total += count; output.write(data, 0, count); } output.flush(); output.close(); input.close(); return file; } catch (IOException e) { e.printStackTrace(); } return null; } }) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Subscriber<File>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(File file) { String authority = activity.getApplicationContext().getPackageName() + ".fileprovider"; Uri uriToFile = FileProvider.getUriForFile(activity, authority, file); Intent shareIntent = new Intent(Intent.ACTION_VIEW); shareIntent.setDataAndType(uriToFile, "application/pdf"); shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); if (shareIntent.resolveActivity(activity.getPackageManager()) != null) { activity.startActivity(shareIntent); } } }); } 

}

For Intent to work, you need to create a FileProvider to grant permission to the receiving application to open the file.

Here's how you implement it: in your manifest:

  <provider android:name="android.support.v4.content.FileProvider" android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider> 

Finally, create file_paths.xml in the resource folder

 <?xml version="1.0" encoding="utf-8"?> <paths> <files-path name="shared_pdf" path="shared_pdf"/> </paths> 

Hope this helps =)

+8
Aug 09 '17 at 15:25
source share

Download from progressDialog here. You need to give WebClient, otherwise it will be forced to open in the browser:

 final ProgressDialog pDialog = new ProgressDialog(context); pDialog.setTitle(context.getString(R.string.app_name)); pDialog.setMessage("Loading..."); pDialog.setIndeterminate(false); pDialog.setCancelable(false); WebView webView = (WebView) rootView.findViewById(R.id.web_view); webView.getSettings().setJavaScriptEnabled(true); webView.setWebViewClient(new WebViewClient() { @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); pDialog.show(); } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); pDialog.dismiss(); } }); String pdf = "http://www.adobe.com/devnet/acrobat/pdfs/pdf_open_parameters.pdf"; webView.loadUrl("https://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf); 
+6
Nov 16 '16 at 7:50
source share

Use this code:

 private void pdfOpen(String fileUrl){ webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setPluginState(WebSettings.PluginState.ON); //---you need this to prevent the webview from // launching another browser when a url // redirection occurs--- webView.setWebViewClient(new Callback()); webView.loadUrl( "http://docs.google.com/gview?embedded=true&url=" + fileUrl); } private class Callback extends WebViewClient { @Override public boolean shouldOverrideUrlLoading( WebView view, String url) { return (false); } } 
+5
Feb 21 '19 at 11:17
source share

You can use the Mozilla project pdf.js. Basically it will show you a PDF. Look at their example .

I use it only in the browser (on the computer and on the mobile device) and it works fine.

+4
May 12 '14 at 12:39
source share

This is the actual usage restriction that Google allows before you receive the error message mentioned in the comments. If this is a PDF file that the user will open in the application, then I feel that it is completely safe. Although it is recommended that you follow the native approach using the Android 5.0 / Lollipop built-in Android framework, it is called PDFRenderer .

0
Jul 19 '17 at 11:54 on
source share

In fact, all the solutions were quite complicated, and I found a really simple solution (I'm not sure if it is available for all versions of the SDK). It will open the PDF document in the preview window, where the user can view and save / share the document:

 webView.setDownloadListener(DownloadListener { url, userAgent, contentDisposition, mimetype, contentLength -> val i = Intent(Intent.ACTION_QUICK_VIEW) i.data = Uri.parse(url) startActivity(i) }) 

(Kotlin)

0
May 11 '19 at 15:34
source share

Download the source code from here ( open PDF in Android WebView )

activity_main.xml

 <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <WebView android:layout_width="match_parent" android:background="#ffffff" android:layout_height="match_parent" android:id="@+id/webview"></WebView> </RelativeLayout> 

MainActivity.java

 package com.pdfwebview; import android.app.ProgressDialog; import android.graphics.Bitmap; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.webkit.WebView; import android.webkit.WebViewClient; public class MainActivity extends AppCompatActivity { WebView webview; ProgressDialog pDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); listener(); } private void init() { webview = (WebView) findViewById(R.id.webview); webview.getSettings().setJavaScriptEnabled(true); pDialog = new ProgressDialog(MainActivity.this); pDialog.setTitle("PDF"); pDialog.setMessage("Loading..."); pDialog.setIndeterminate(false); pDialog.setCancelable(false); webview.loadUrl("https://drive.google.com/file/d/0B534aayZ5j7Yc3RhcnRlcl9maWxl/view"); } private void listener() { webview.setWebViewClient(new WebViewClient() { @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); pDialog.show(); } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); pDialog.dismiss(); } }); } } 
-one
Mar 23 '17 at 12:15
source share

This worked for me:

 webViewReportViewing.loadUrl("http://docs.google.com/gview?embedded=true&url=" +reportFileUrl); 
-one
Apr 26 '17 at 6:16
source share



All Articles