Display the pdf file(stored in google drive) in webview by using google docs
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.)
Comments
-
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
- I've downloaded the pdf doc
- uploaded in google drive
- Got my own link for pdf
- Trying to open that in
webview
using google docs - 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.
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 over 7 yearsYes, 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 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 over 7 years@Prabs, google drive will convert any PDF into HTML, so you'll have to upload it somewhere else, I think
-
Prabs over 7 yearsdrive.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 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 over 7 yearsYes, I see. is there any other way to host our pdf files
-
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 over 6 yearswill it display or download it to device?
-
Anish Bhandari over 6 yearsIt will display the pdf in webview
-
Jilson P Jose over 3 yearsWe just want to add /view after the sharable link of the pdf file from google drive... Thank u so much
-
Pooja over 3 yearsPerfect! Thanks