Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

  • pdf stored in other website link: "http://www.pdf995.com/samples/pdf.pdf"

  • Sharable link of pdf saved in google drive: "https://drive.google.com/open?id=0B8e4zX5Y1S0XV0U0UTZJOUVma00"

  • If it's preceded with google docs : "https://docs.google.com/gview?embedded=true&url=https://drive.google.com/file/d/0B8e4zX5Y1S0XV0U0UTZJOUVma00/view"

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. :(

like image 489
Prabs Avatar asked Aug 30 '16 07:08

Prabs


2 Answers

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
like image 196
Phúc Nghi Lâm Avatar answered Sep 30 '22 17:09

Phúc Nghi Lâm


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);
like image 25
Vladyslav Matviienko Avatar answered Sep 30 '22 17:09

Vladyslav Matviienko