You can use Google PDF Viewer to read your pdf online:
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("https://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);
If you use the view only url the user is not propted to login to there google account.
https://docs.google.com/viewer?url=http://my.domain.com/yourPdfUrlHere.pdf
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);
}
}
Opening a pdf using google docs is a bad idea in terms of user experience. It is really slow and unresponsive.
Since api 21, we have PdfRenderer which helps converting a pdf to Bitmap. I've never used it but is seems easy enough.
Other solution is to download the PDF and pass it via Intent to a dedicated PDF app which will do a banger job displaying it. Fast and nice user experience, especially if this feature is not central in your app.
Use this code to download and open the 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 the Intent to work, you need to create a FileProvider to grant permission to the receiving app to open the file.
Here is 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 a file_paths.xml file in the resources foler
<?xml version="1.0" encoding="utf-8"?>
<paths>
<files-path name="shared_pdf" path="shared_pdf"/>
</paths>
Hope this helps =)
Here load with progressDialog. Need to give WebClient otherwise it force to open in 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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With