PDF display using Google Docs viewer in WebView not working

I’m having trouble displaying a PDF file through Google Docs viewer inside a WebView component. Following suggestions from other developers, I decided to use Google’s document viewer service to render PDF content.

@SuppressLint("SetJavaScriptEnabled")
public void loadPdfDocument(final String documentUrl) {
    WebView pdfWebView = (WebView) findViewById(R.id.pdf_webview);
    pdfWebView.getSettings().setJavaScriptEnabled(true);
    pdfWebView.getSettings().setPluginsEnabled(true);
    pdfWebView.loadUrl("https://docs.google.com/viewer?url=http://www.example.com/sample-document.pdf");
}

The PDF file opens perfectly when I test it in my desktop browser, but when I run the same code on my Android device or emulator, I get a “Webpage not available” error message. I’m testing this on Android 4.0.

Am I missing some configuration or permission that’s needed for this to work on mobile devices?

First, check if you’ve got INTERNET permission in your AndroidManifest.xml - WebView won’t load external URLs without it. Android 4.0’s security policies are pretty strict and might block mixed content or certain resources. Try using a direct PDF URL instead of going through Google Docs viewer. I’ve seen corporate networks and device configs block Google services, which would explain why it works on desktop but not mobile. You might also want to set a desktop user agent string since Google often serves different content based on that.

This happens all the time with older Android versions. Google Docs viewer needs a newer WebView than what Android 4.0 has. The built-in browser on that version can’t handle the JavaScript and rendering that Google’s viewer needs.

You’ll need a fallback for older devices. I’d detect the Android version and use a different PDF method for anything below API 19. You could try a third-party library like AndroidPdfViewer or just let users download the PDF and open it in another app. Also, that setPluginsEnabled method is deprecated - removing it might fix some compatibility problems.

i’ve had similar probs on 4.0 too. maybe just try a different viewer, or consider saving the pdf locally first and opening it. less hassle than dealing with google docs.