Is there any way to load PDF file from a server into a WebView, without downloading the PDF to the device or using google docs to open it?
I have been looking around but can not find any solution. I have also tried the code below (Source):
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();
}
});
}
}
but when changing the loadUrl link to my PDF file destination, the file doesn't show up either.
The required API is 15 and above.
I've seen good results in some apps I've worked on, using AndroidPdfViewer. It adds some MB to your final APK, but trust me it's worth it. It can render just about any pdf file and has great performance. You'll have to download the PDF to the device, but you can delete it afterwards.
It's currently the best way, and most compatible with older android versions, of loading pdf files without leaving the app.
You can read here about the pros and cons of all the currently available solutions, and how AndroidPdfViewer is king among them :D