Android PDF not loading in browser and WebView

42,256

Solution 1

This is not a complete answer. I investigated several possibilities but still do not have a convincing explanation for this behavior.

Theory A: URL encoding ... no

My first thought was the same as @gn1 - the curly brackets needed URL encoding. Unfortunately, encoding the url parameter doesn't change the result.

Theory B: Bad PDF file ... no

Next I thought that perhaps Google Docs can't handle this particular PDF file - it's an unsupported version or uses optional features or even has errors that other viewers tolerate. I downloaded a copy and hosted it on another site. When I pointed Google Docs to the new location, it previewed fine.

Theory C: Uncooperative site ... no

Next I thought that maybe this site doesn't cooperate with Google Docs for some reason - perhaps it is blocking Google from indexing it. I found another PDF link on the same site with the same scheme:

http://www.expertagent.co.uk/asp/in4glestates/%7B6dad6f28-a59d-4b54-b277-52f077f4927f%7D/%7Be3a6d17c-d6a8-4e92-8f52-09c6721515fc%7D/External.pdf

When I pointed Google Docs to this link, it previewed fine.

Theory D: Different metadata ... no

Now I had one link that worked and one that didn't. Maybe their metadata was different - e.g. perhaps the one that worked was tagged application/pdf and the other one wasn't. So I looked at the HTTP headers.

Working URL:

HTTP/1.1 200 OK
Cache-Control: max-age=3600
Content-Length: 1767120
Content-Type: application/pdf
Last-Modified: Fri, 02 Nov 2007 12:45:00 GMT
Accept-Ranges: bytes
ETag: "46b1592e4e1dc81:13e6"
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
Date: Wed, 03 Jun 2015 23:25:23 GMT

Not working URL:

HTTP/1.1 200 OK
Cache-Control: max-age=3600
Content-Length: 4623702
Content-Type: application/pdf
Last-Modified: Mon, 11 May 2015 15:53:16 GMT
Accept-Ranges: bytes
ETag: "acac5d9828cd01:13e6"
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
Date: Wed, 03 Jun 2015 23:25:42 GMT

I see no apparent significant differences in metadata.

Theory E: Weird request from Google ... no

I'm admittedly grasping at straws at this point. I wondered if the request from Google was different somehow - maybe it asked for a byte range or obscure compression or something, and the target server didn't handle it consistently well. So I pointed Google Docs at a site I control to see what the HTTP request looked like:

GET /asp/in4glestates/%7B16D968D6-198E-4E33-88F4-8A85731CE605%7D/%7B05c36123-4df0-4d7d-811c-8b6686fdd526%7D/External.pdf HTTP/1.1
Host: www.example.com:55555
Connection: Keep-alive
Accept-Encoding: gzip,deflate
User-Agent: Mozilla/5.0 (compatible; Google AppsViewer; http://drive.google.com)

That isn't weird at all. I did verify that the target site ignores compression requests so it isn't a case of buggy compression. I also tried accessing the target site with that User-Agent header and it didn't seem to matter.


So I've found clues that help tell us what isn't the problem but nothing yet that explains what is. I'm posting in hopes that these negative results will still be useful to someone.

Solution 2

I'm using this and works for me: http://weimenglee.blogspot.com/2013/05/android-tip-displaying-pdf-document.html

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    WebView webView=new WebView(MainActivity.this);
    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());

    String pdfURL = "http://www.expertagent.co.uk/asp/in4glestates/{16D968D6-198E-4E33-88F4-8A85731CE605}/{05c36123-4df0-4d7d-811c-8b6686fdd526}/external.pdf";
    webView.loadUrl(
            "http://docs.google.com/gview?embedded=true&url=" + pdfURL);

    setContentView(webView);
}

private class Callback extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(
            WebView view, String url) {
        return(false);
    }
}

Solution 3

Checkout the example code for open PDF without download, in webview .

private void init()
{
    WebView webview = (WebView) findViewById(R.id.webview);
    WebSettings settings = webview.getSettings();
    settings.setJavaScriptEnabled(true);
    webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

    PdfWebViewClient pdfWebViewClient = new PdfWebViewClient(this, webview);
    pdfWebViewClient.loadPdfUrl(
            "https://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0ahUKEwjgwIfp3KXSAhXrhFQKHQqEDHYQFggZMAA&url=http%3A%2F%2Fwww.orimi.com%2Fpdf-test.pdf&usg=AFQjCNERYYcSfMLS5ukBcT2Qy11YxEhXqw&cad=rja");
}


private class PdfWebViewClient extends WebViewClient
{
   private static final String TAG = "PdfWebViewClient";
   private static final String PDF_EXTENSION = ".pdf";
   private static final String PDF_VIEWER_URL = "http://docs.google.com/gview?embedded=true&url=";

   private Context mContext;
   private WebView mWebView;
   private ProgressDialog mProgressDialog;
   private boolean isLoadingPdfUrl;

   public PdfWebViewClient(Context context, WebView webView)
   {
      mContext = context;
      mWebView = webView;
      mWebView.setWebViewClient(this);
   }

   public void loadPdfUrl(String url)
   {
      mWebView.stopLoading();

      if (!TextUtils.isEmpty(url))
      {
          isLoadingPdfUrl = isPdfUrl(url);
          if (isLoadingPdfUrl)
          {
              mWebView.clearHistory();
          }

          showProgressDialog();
       }

      mWebView.loadUrl(url);
  }

  @SuppressWarnings("deprecation")
  @Override
  public boolean shouldOverrideUrlLoading(WebView webView, String url)
  {
      return shouldOverrideUrlLoading(url);
  }

  @SuppressWarnings("deprecation")
  @Override
  public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl)
  {
      handleError(errorCode, description.toString(), failingUrl);
  }

  @TargetApi(Build.VERSION_CODES.N)
  @Override
  public boolean shouldOverrideUrlLoading(WebView webView, WebResourceRequest request)
  {
      final Uri uri = request.getUrl();
      return shouldOverrideUrlLoading(webView, uri.toString());
  }

  @TargetApi(Build.VERSION_CODES.N)
  @Override
  public void onReceivedError(final WebView webView, final WebResourceRequest request, final WebResourceError error)
  {
      final Uri uri = request.getUrl();
      handleError(error.getErrorCode(), error.getDescription().toString(), uri.toString());
  }

  @Override
  public void onPageFinished(final WebView view, final String url)
  {
      Log.i(TAG, "Finished loading. URL : " + url);
      dismissProgressDialog();
  }

  private boolean shouldOverrideUrlLoading(final String url)
  {
      Log.i(TAG, "shouldOverrideUrlLoading() URL : " + url);

      if (!isLoadingPdfUrl && isPdfUrl(url))
      {
          mWebView.stopLoading();

          final String pdfUrl = PDF_VIEWER_URL + url;

          new Handler().postDelayed(new Runnable()
          {
              @Override
              public void run()
              {
                  loadPdfUrl(pdfUrl);
              }
          }, 300);

          return true;
      }

      return false; // Load url in the webView itself
  }

   private void handleError(final int errorCode, final String description, final String failingUrl)
  {
      Log.e(TAG, "Error : " + errorCode + ", " + description + " URL : " + failingUrl);
  }

  private void showProgressDialog()
  {
      dismissProgressDialog();
      mProgressDialog = ProgressDialog.show(mContext, "", "Loading...");
  }

  private void dismissProgressDialog()
  {
      if (mProgressDialog != null && mProgressDialog.isShowing())
      {
          mProgressDialog.dismiss();
          mProgressDialog = null;
      }
  }

  private boolean isPdfUrl(String url)
  {
      if (!TextUtils.isEmpty(url))
      {
          url = url.trim();
          int lastIndex = url.toLowerCase().lastIndexOf(PDF_EXTENSION);
          if (lastIndex != -1)
          {
              return url.substring(lastIndex).equalsIgnoreCase(PDF_EXTENSION);
          }
      }
    return false;
  }
}

Solution 4

Here is working solution for 'No Preview Available' issue.. Exact Problem is in URL encoding which we concat with "http://docs.google.com/gview?url=". This means we have to replace all special character(:, /, & etc) of url with unicode. Uri.encode("") do the trick for us.

String url = Uri.encode("your link");
    String finalUrl = "http://docs.google.com/viewer?url=" + url + "&embedded=true";

    WebSettings webSettings = webView.getSettings();
    webSettings.setBuiltInZoomControls(true);
    webSettings.setJavaScriptEnabled(true);
    webView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);

    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setUseWideViewPort(true);
    webView.getSettings().setLoadWithOverviewMode(true);

    progressView.setVisibility(View.VISIBLE);
    webView.loadUrl(finalUrl);

    webView.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageFinished(WebView view, String url) {
            view.getSettings().setLoadsImagesAutomatically(true);
            webView.setVisibility(View.VISIBLE);
            //progressView.setVisibility(View.VISIBLE);

            if (progressView != null && progressView.isShown()) {
                progressView.setVisibility(View.GONE);
            }

            Log.v("after load", view.getUrl());
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {

        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Toast.makeText(getApplicationContext(), description, Toast.LENGTH_SHORT).show();
            Log.e("error", description);

        }
    });
Share:
42,256

Related videos on Youtube

Abdul Mohsin
Author by

Abdul Mohsin

An application developer, having knowledge in Java programming language, web application backend design/development, Spring framework

Updated on April 20, 2022

Comments

  • Abdul Mohsin
    Abdul Mohsin about 2 years

    I am trying to load PDF files in Android Webview. when i Googled it. the best answer what i found is to use Google Docs. Now What i did is append the PDF file URL at the end of this url https://docs.google.com/gview?embedded=true&url=

    and then load this complete URL in the android WebView. it loads the PDF successfully. But there is one PDF file on the following URL that is not loading in WebView as well as in Chrome browser (on my system). The PDF URL is

    http://www.expertagent.co.uk/asp/in4glestates/{16D968D6-198E-4E33-88F4-8A85731CE605}/{05c36123-4df0-4d7d-811c-8b6686fdd526}/external.pdf

    and When i try to load the PDF as https://docs.google.com/gview?embedded=true&url=www.expertagent.co.uk/asp/in4glestates/{16D968D6-198E-4E33-88F4-8A85731CE605}/{05c36123-4df0-4d7d-811c-8b6686fdd526}/external.pdf

    then it says No Preview Available. can anyone please tell me whats wrong Here.

    • gn1
      gn1 almost 9 years
      You probably need to escape the PDF url and then append it to the Google URL.
    • Abdul Mohsin
      Abdul Mohsin almost 9 years
      how to escape the pdf url. can you please tell it?
    • gn1
      gn1 almost 9 years
      I thought it was the curly brackets. Apparently, it is not. A Google docs issue.
  • Abdul Mohsin
    Abdul Mohsin almost 9 years
    i dont want to open any another app. i want to load that PDF in my Existing WebView
  • Abdul Mohsin
    Abdul Mohsin almost 9 years
    thanks for your efforts. and now i notice that that pdf is opening fine in Google Docs. dont know how but it is working fine. can you please check it again in google docs
  • rhashimoto
    rhashimoto almost 9 years
    No, I still see exactly the same behavior here as before.
  • gn1
    gn1 almost 9 years
    The robots.txt of the site excludes the directory.
  • Abdul Mohsin
    Abdul Mohsin almost 9 years
    not worked :( . i think there is no particular solution for this kind of problem
  • rhashimoto
    rhashimoto almost 9 years
    @gn1 Good observation. Unfortunately it still doesn't explain the observations - one document under that directory works and another one doesn't. I also do not see Docs checking robots.txt when a request is made to my own server.
  • gn1
    gn1 almost 9 years
    It probably has your robots.txt in its cache. I put the file on my server it works fine. Abdul, put the file in an ordinary folder and link to it. Avoid the weird characters in the path.
  • gautam
    gautam over 6 years
    Is there any solution to this problem? i have encountered the same problem , i think it is showing "no preview available" in http website more than https.
  • uberchilly
    uberchilly over 5 years
    It is interesting that webView.getSettings().setPluginState(WebSettings.PluginState‌​.ON); helped, but it is unfortunately deprecated. But it only helped me with links that doesn't have .pdf as part of the url. And I have tested direct pdf link, not docs version