Display the pdf file(stored in google drive) in webview by using google docs

14,343

Solution 1

I have a WebView, and the link to my pdf stored in google drive is:

String myPdfUrl = "https://drive.google.com/file/d/1fQfqgEmz-AiCpdHEIh7SNwdnAkQFqDQp/view";

Don't forget "/view".

And my java code for display:

private void displayWebView() {
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebViewClient(new WebViewClient(){
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            view.loadUrl(myPdfUrl);
            return true;
        }
    });
    webView.loadUrl(myPdfUrl);
}

And add @SuppressLint("SetJavaScriptEnabled") above onCreate()

@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.act

Solution 2

you can view any pdf on the internet using google docs even without downloading it to the device. Here is the sample of how to do it:

webview.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);

Where pdf is a string link to your PDF file. So in your case it will be:

String pdf = "http://www.pdf995.com/samples/pdf.pdf";
webview.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);

Solution 3

Define Url like:

String BASE_URL = "https://drive.google.com/viewerng/viewer?embedded=true&url=https://drive.google.com/uc?id=";
String PDF_ID = "<Your pdf id from Google drive share link>";

Load in WebView:

webview.loadUrl(BASE_URL + PDF_ID);

Solution 4

First, you need to follow the answer provided by "Phúc Nghi Lâm." (the 3rd answer.)

If it is not working....

In my situation, there is HTML-result coming. But your WebView's size is 0-width and 0-height.

So you need to reset the LayoutParam by implement the "onPageFinished." Or something else.

(You should be able to get the parent-view of the WebView. So.. 1.Get the size of parent-view. 2.Set the size data into a new LayoutParam. 3. Set the new LayoutParam to the WebView.)

Share:
14,343
Prabs
Author by

Prabs

Learner and Developer

Updated on June 07, 2022

Comments

  • Prabs
    Prabs almost 2 years

    My pdf file is stored in some website ex: http://www.pdf995.com/samples/pdf.pdf

    Now I can't rely on other websites for my app. So

    1. I've downloaded the pdf doc
    2. uploaded in google drive
    3. Got my own link for pdf
    4. Trying to open that in webview using google docs
    5. FAILED

    Now the links are as follows

    Result,

    • Other websites may change their url at anytime. Can't rely on it.

    • If I use the google drive link alone, then it is opening in web browser not within the app.

    • If preceded with google docs, it is not resulting in a pdf doc. I get something like this.

    enter image description here

    What should be done so that I can use my own pdf from google drive to open in webview

    I've followed CommonsWare, Stuart Siegler, Samir Mangroliya But nothing works. :(

  • Prabs
    Prabs over 7 years
    Yes, you are right @Vlad. But, I don't want to rely on other website. That's the reason I've opted for 'Google Drive'
  • Vladyslav Matviienko
    Vladyslav Matviienko over 7 years
    @Prabs, the reason it shows HTML code is that the link drive.google.com/file/d/0B8e4zX5Y1S0XV0U0UTZJOUVma00/view is not a PDF anymore. It is an HTML built from the PDF.
  • Vladyslav Matviienko
    Vladyslav Matviienko over 7 years
    @Prabs, google drive will convert any PDF into HTML, so you'll have to upload it somewhere else, I think
  • Prabs
    Prabs over 7 years
    drive.google.com/file/d/0B8e4zX5Y1S0XV0U0UTZJOUVma00/view is a pdf format. but when I join it to google docs, it is in HTML. 'Somewhere else'? you mean by hosting server. I can't go with free hosting servers as they have their own list of issues.
  • Vladyslav Matviienko
    Vladyslav Matviienko over 7 years
    @Prabs, if you see the source of this page drive.google.com/file/d/0B8e4zX5Y1S0XV0U0UTZJOUVma00/view you'll see, that google drive converted your PDF to HTML, and this link does not provide a PDF anymore.
  • Prabs
    Prabs over 7 years
    Yes, I see. is there any other way to host our pdf files
  • Vladyslav Matviienko
    Vladyslav Matviienko over 7 years
    @Prabs, I don't know such a way since google drive does not give direct links to the stored files.
  • Prabs
    Prabs over 6 years
    will it display or download it to device?
  • Anish Bhandari
    Anish Bhandari over 6 years
    It will display the pdf in webview
  • Jilson P Jose
    Jilson P Jose over 3 years
    We just want to add /view after the sharable link of the pdf file from google drive... Thank u so much
  • Pooja
    Pooja over 3 years
    Perfect! Thanks