I try to open the pdf in WebView from url, but it is not opening.It showing no preview available. It opening correctly on web. I also enable the JavaScriptEnabled.I try it for last one day but not able to oprn the pdf.Coud anyone please help me in it. I use this pdf link to open in WebView
Asked
Active
Viewed 189 times
0
-
try opening it with google doc – oziomajnr Oct 03 '18 at 06:45
-
2Possible duplicate of [How can I display a pdf document into a Webview?](https://stackoverflow.com/questions/2655972/how-can-i-display-a-pdf-document-into-a-webview) – V-rund Puro-hit Oct 03 '18 at 06:45
-
show your code.. @Rishikesh Rahi – Ankita Oct 03 '18 at 06:47
-
@Ankita i follwed this way webview.getSettings().setJavaScriptEnabled(true); String filename ="http://54.64.229.48//dev//sites//default//files//final_features%20for%20Oh%2527%20My%20Concierge_29_06_2018.pdf"; webview.loadUrl("http://docs.google.com/gview?embedded=true&url=" + filename); webview.setWebViewClient(new WebViewClient() { public void onPageFinished(WebView view, String url) { // do your stuff here progressbar.setVisibility(View.GONE); } }); – Rishikesh Rahi Oct 03 '18 at 06:53
-
Try to check either the URL of file is not null like (https://docs.google.com/gview?embedded=true&url=null) – Ankita Oct 03 '18 at 06:59
-
@ Ankita the pdf is opening in web you can also check it i am giving you the pdf link :- http://54.64.229.48//dev//sites//default//files//final_features%20for%20Oh%2527%20My%20Concierge_29_06_2018.pdf – Rishikesh Rahi Oct 03 '18 at 07:09
3 Answers
0
Use google docs to open PDF in webview like below
https://docs.google.com/gview?embedded=true&url=http://54.64.229.48/dev/sites/default/files/final_features%2520for%2520Oh%252527%2520My%2520Concierge_29_06_2018.pdf
hope it helps.

Shamsul
- 435
- 6
- 15
-
-
@Rishikesh please copy and paste same URL as below https://docs.google.com/gview?embedded=true&url=http://54.64.229.48/dev/sites/default/files/final_features%2520for%2520Oh%252527%2520My%2520Concierge_29_06_2018.pdf – Shamsul Oct 03 '18 at 07:10
-
-
Its working good on my side, If you have noticed that there is a minor change in your pdf URL that I mentioned in the comment, please paste comment's URL as it is. – Shamsul Oct 03 '18 at 07:20
-
..what have you do with the original url..can you please explain a bit – Rishikesh Rahi Oct 03 '18 at 07:30
-
your pdf URL contain emply space, avoid empty space in URL. I have replaced % to Unicode char with %25 refer this link http://www.greentea.markschwing1.com/unico.html If this answer working for you please accept this answer. – Shamsul Oct 03 '18 at 08:15
-
actually url comes from backened..so how do i avoid the empty space. Please crarify me in it. – Rishikesh Rahi Oct 03 '18 at 08:32
-
i have just joined the stack overflow and my reputation is less than 15 that's why i enable to vote you. If i can the i definitely vote you 5 for this. – Rishikesh Rahi Oct 03 '18 at 08:46
0
String pdfUrl="http://54.64.229.48/dev/sites/default/files/final_features%20for%20Oh%2527%20My%20Concierge_29_06_2018.pdf";
String url = "http://docs.google.com/gview?embedded=true&url=" + pdfUrl;
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);

Md.Tarikul Islam
- 1,241
- 1
- 14
- 16
-
still it is not working. when using above code it redirected to web page and showing no preview available. – Rishikesh Rahi Oct 03 '18 at 07:04
0
Use this code and let me know if it works for you Remember it will not open pdf in WebView
Intent viewPdfIntent = new Intent(Intent.ACTION_VIEW);
viewPdfIntent.setData(Uri.parse("http://54.64.229.48/dev/sites/default/files/final_features%20for%20Oh%2527%20My%20Concierge_29_06_2018.pdf"));
viewPdfIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
if (viewPdfIntent.resolveActivity(getPackageManager()) != null) {
startActivity(viewPdfIntent);
}
And for WebView you can use
private void setUpWebView(String documentUrl) {
webView.setWebViewClient(new AppWebViewClients());
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setUseWideViewPort(false);
webView.loadUrl("http://docs.google.com/gview?embedded=true&url="
+ documentUrl);
}
and
public class AppWebViewClients extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
}
}
Don't Forget to add in manifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Sandeep Parish
- 1,888
- 2
- 9
- 20
-
it is opening but i want to open in webView. How do i open it in webView? – Rishikesh Rahi Oct 03 '18 at 07:07
-
private void setUpWebView(String documentUrl) { webview.setWebViewClient(new AppWebViewClients()); webview.getSettings().setJavaScriptEnabled(true); webview.getSettings().setUseWideViewPort(false); webview.loadUrl("http://docs.google.com/gview?embedded=true&url=" + documentUrl); } private class AppWebViewClients extends WebViewClient { } – Rishikesh Rahi Oct 03 '18 at 07:19
-
String pdfUrl="http://54.64.229.48/dev/sites/default/files/final_features%20for%20Oh%2527%20My%20Concierge_29_06_2018.pdf"; – Rishikesh Rahi Oct 03 '18 at 07:20
-
My Mistake i forget to add `AppWebViewClients` check updated answer – Sandeep Parish Oct 03 '18 at 07:22
-
-
but i have solved by using this link:- https://docs.google.com/gview?embedded=true&url=http://54.64.229.48/dev/sites/default/files/final_features%2520for%2520Oh%252527%2520My%2520Concierge_29_06_2018.pdf – Rishikesh Rahi Oct 03 '18 at 07:27
-
-
-
the url contains the empty space..for that i have to customize the url. That's why it isnot working coreectly in webView – Rishikesh Rahi Oct 03 '18 at 08:34