I'm using a Web View to display a site inside my app, and that site contains links to PDF files. But when this links are pressed the Web View displays a blank page instead of the PDF files. Is there anyway I can get it to display the PDF files correctly?
Thanks in advance.
If possible, you can display PDF documents and visit online websites using the add-in: import 'dart:html' as html; html. window. open('http:///www.website.com/document.pdf');
You can create an intent to open the pdf files:
@Override
public boolean launchPDF(WebView view, String url) {
if ( urlIsPDF(url)){
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(url), "application/pdf");
try{
view.getContext().startActivity(intent);
} catch (ActivityNotFoundException e) {
//user does not have a pdf viewer installed
}
} else {
webview.loadUrl(url);
}
return true;
}
And then whenever a user clicks a PDF link in a page within your webview, the file will open in an external PDF app.
Or you can use Google Docs to launch them:
String googleDocs = "https://docs.google.com/viewer?url=";
String pdf_url = "http://www.somedomain.com/new.pdf";
webView.loadUrl(googleDocs + pdf_url);
Remember to use the Internet Permission.
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