6

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

Community
  • 1
  • 1
Prabs
  • 4,923
  • 6
  • 38
  • 59

5 Answers5

7

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
Phúc Nghi Lâm
  • 140
  • 1
  • 8
3

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);
Vladyslav Matviienko
  • 10,610
  • 4
  • 33
  • 52
  • 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' – Prabs Aug 30 '16 at 07:26
  • @Prabs, the reason it shows HTML code is that the link https://drive.google.com/file/d/0B8e4zX5Y1S0XV0U0UTZJOUVma00/view is not a PDF anymore. It is an HTML built from the PDF. – Vladyslav Matviienko Aug 30 '16 at 07:30
  • @Prabs, google drive will convert any PDF into HTML, so you'll have to upload it somewhere else, I think – Vladyslav Matviienko Aug 30 '16 at 07:32
  • https://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. – Prabs Aug 30 '16 at 07:37
  • 2
    @Prabs, if you see the source of this page http://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. – Vladyslav Matviienko Aug 30 '16 at 07:43
  • Yes, I see. is there any other way to host our pdf files – Prabs Aug 30 '16 at 08:00
  • 1
    @Prabs, I don't know such a way since google drive does not give direct links to the stored files. – Vladyslav Matviienko Aug 30 '16 at 08:01
2

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);
Deven
  • 3,078
  • 1
  • 32
  • 34
1

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.)

hsu.tw
  • 148
  • 2
  • 10
-1

I was suffering from same problem and after a lot of hit and trials, I found a way out.

You need to click on download button of google drive.

as soon as you click it, a new tab will open with a different url, of this type

https://drive.google.com/uc?id=0B1dndMpNP4zgdlTuMm5rcjFk3TQ&export=download //test url//

String url="https://drive.google.com/uc?id=0B1dndMpNP4zgdlTuMm5rcjFk3TQ&export=download";
webView = (WebView) findViewById(R.id.progr);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url="+url);