Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display pdf when link clicked in WebView android

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.

like image 884
Andres Avatar asked Jul 21 '15 17:07

Andres


People also ask

How do I open PDF in WebView flutter?

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');


1 Answers

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.

like image 63
Ahmed Avatar answered Nov 16 '22 19:11

Ahmed