Android WebView displays 'no preview available' error when trying to view PDFs through Google Docs

I am attempting to use WebView in my Android application to showcase PDF documents via Google Docs. While this usually works well, I occasionally encounter a ‘no preview available’ error, and I’m unsure about the reason.

Here’s my code snippet:

pdfViewer.getSettings().setJavaScriptEnabled(true);
pdfViewer.getSettings().setBuiltInZoomControls(true);

loadingSpinner.setVisibility(View.VISIBLE);

String pdfUrl = "http://docs.google.com/gview?embedded=true&url=" + getIntent().getStringExtra(PDF_URL_KEY);
pdfViewer.loadUrl(pdfUrl);

pdfViewer.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        loadingSpinner.setVisibility(View.VISIBLE);
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        loadingSpinner.setVisibility(View.GONE);
        pdfViewer.setVisibility(View.VISIBLE);
    }

    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        view.loadUrl("about:blank");
        Toast.makeText(MainActivity.this, "Error loading PDF", Toast.LENGTH_SHORT).show();
        super.onReceivedError(view, errorCode, description, failingUrl);
    }
});

Interestingly, I find that certain PDFs consistently produce this error, while others do not. Additionally, it seems that files from HTTP sources fail more frequently than those from HTTPS. There are instances when the progress indicator just halts without displaying any content.

What leads to this ‘no preview available’ message, and how can I resolve this? Furthermore, is there a way to convert an HTTP URL to HTTPS without altering the website?

I’ve dealt with this before - it’s usually CORS policies or PDF file issues that Google’s viewer can’t handle. The HTTP vs HTTPS thing you’re seeing? That’s mixed content blocking. Browsers won’t load HTTP resources from HTTPS pages anymore, so Google can’t fetch your PDFs.

Here’s what actually works: set up a fallback system instead of fighting with Google’s viewer. Check if the viewer fails by looking for Google’s error content, then automatically switch to direct download or a different viewer.

Also try adding proper headers when you serve PDFs - Content-Disposition and Content-Type seem to help Google process files better. Those timeouts mean Google’s servers can’t access or process your PDF fast enough.

totally agree, google’s viewer can be super unreliable! switching to pdf.js or offering a direct download might be your best bet. mixed content issues can really mess things up too, especially with http links.

The ‘no preview available’ error generally occurs when the Google Docs viewer encounters issues with the PDF file, such as file size limitations (typically over 25MB), corruption, or restrictions from the host server, which may block Google’s access. Unfortunately, you cannot convert HTTP links to HTTPS without the server’s cooperation. A better approach is to start with HTTPS links and revert to HTTP if that fails. Additionally, consider implementing a timeout handler, as the Google viewer tends to stall with problematic files. I’ve also found success in changing the user agent in the WebView settings, which can help, as some servers reject the default Android user agent. Be sure to inspect the loaded page for Google’s specific error messages rather than relying solely on WebView’s error callbacks.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.