Android open pdf file from Url and diaplay in web view? - android

I know this question has been asked so many time , but i didn't get any proper and satisfactory solution. I want to show the Pdf file in web view , which is coming from the url ,when i click on the URL , my pdf downloads. Currently i am usinghtml following code for showing pdf file in web view , but it is opening the html file. I am getting stuck in this since 4 hours.Kindly suggest me where i am doing wrong.
Thanks in advance.
mWebview.getSettings().setJavaScriptEnabled(true);
WebSettings settings = mWebview.getSettings();
settings.setDefaultTextEncodingName("utf-8");
mWebview.loadData(response, "application/pdf; charset=utf-8",null);
i have following response coming in following format :-
**%PDF-1.4%���5 0 obj<</E 74937/H [ 1453 173 ]/L 75330/Linearized 1/N 1/O 8/T 75180>>**

I want to show the Pdf file in web view
WebView does not have a PDF renderer.
Kindly suggest me where i am doing wrong.
You are attempting to load a PDF directly into a WebView. Use something else to render the PDF.

For show pdf in webview use Direct url:
https://docs.google.com/gview?embedded=true&url="PDF_FILE_URL"
Now load above url in webview.
mWebview.loadUrl("https://docs.google.com/gview?embedded=true&url="PDF_FILE_URL"");

Related

How to loaded survey form dynamically in android from url?

i need to know how to load Survey Form dynamically from my web url and show it in android.
For that you have to use WebView.
in your OnCreate() method use following code
WebView browser = (WebView) findViewById(R.id.webview);
browser.loadUrl("http://www.tutorialspoint.com");
Tutorial for implementing the complete solution : Open URL in WebView.

How to clear html page before showing into a webview in Android?

I have the URL of a webpage to be displayed into a webview in my Android app. Before showing this page i want to clear the html code of this page from some tag (such as the header, footer, ecc..) in order to show only few information. How can i do it? I tried to solve the issue working with JSoup but i can't understand how to create and pass the "new page" to the webview. Anybody can help me?
EDIT
I cleaned the html code useless through jsoup libraries. Then, always by mean of these, i get head and body content and finally i showing the "cleared" web page through these lines:
headURL = doc.select("head").outerHtml();
bodyURL = doc.select("body").outerHtml();
webview.loadData( "<html>"+headURL+bodyURL+"</html>" , "text/html", "charset=UTF-8");
webview.setWebViewClient(new DisPlayWebPageActivityClient());
The view shows the new page but do not load css files specified in the head(that has not been touched). Who can say me why?
You can fetch the WebPage you want to display as a string, parse and remove whatever you don't want and then load this string as data in your webview.
Something like:
String webContent = fetchPage(url);
String cleanedWebContent = cleanUp(webContent);
webView.loadData(cleanedWebContent, "text/html", "UTF-8");
Of course, you will need to implement fetchPage and cleanUp as they are not Android methods

POST to Android Webview where the response is a PDF file

I am performing a POST to the Android webview where the expected response is a PDF file. However the webview just shows a blank page. I realise that PDF files cannot be rendered in the webview but would expect the file to start downloading or show some response at least.
Does anyone know if its possible to POST to a webview to initiate download of a (pdf) file?
_webView = FindViewById<WebView>(Resource.Id.ebookWebview);
_webView.SetWebViewClient(webviewClient);
_webView.SetWebChromeClient(new WebChromeClient());
_webView.Settings.AllowFileAccess = true;
_webView.Settings.JavaScriptEnabled = true;
_webView.PostUrl(_postUrl, EncodingUtils.GetBytes(_postData, "BASE64"));
Have you tried registering a DownloadListener on the WebView? That is needed to handle the download. Please see http://developer.android.com/reference/android/webkit/WebView.html#setDownloadListener(android.webkit.DownloadListener)

how to display a pdf file in Web view without storing it in SDCard

I am realizing an android project I am blocked that one I wanted to show a pdf in webview .the pdf file is accessible via an URL in localhost.
when I use this code, webView.loadUrl("https://docs.google.com/gview?embedded=true&url="+pdfurl);
it shows :
"Sorry, we were unable to find the document at the original source. Verify that the document still exists.You can also try to download the original document by clicking here."
Help me to solve this.Thanks in advance.
Your file is located in your local server.It cant accessed from outside your network.Upload your pdf file to any website like this or this.And get the public url from there.Then try to access that url from your application.
You can use the google's document viewer service for viewing the pdf,ppt contents in the webview if your document resource is on online.
create following HTML string like :
String googleDocUrl ="<iframe src='http://docs.google.com/gview?url="
+ YOUR_ONLINE_PDF_DOC_URL
+ "&embedded=true' width='100%' height='100%'style='border: none;'></iframe>";
call webviews loadData() method :
mWebView.loadData(googleDocUrl ,"text/html","UTF-8");
Hope this might help you :)

Android - Problem in using the Google pdf viewer in webview

I have seen lot of threads about loading a PDF in Android Webview. But I didn't see the issue what I am getting.
I am using Google PDF viewer to load the URL in the Webview which will give a PDF as response.
I am setting the following properties to my webview
mWebview.setWebViewClient(new WebViewClient());
mWebview.setVerticalScrollBarEnabled(false);
mWebview.getSettings().setJavaScriptEnabled(true);
Webview.getSettings().setDefaultZoom(ZoomDensity.MEDIUM);
String url = "http://docs.google.com/viewer?embedded=true&url=" + "my url";
mWebview.loadUrl(url);
I am able load the PDF with the above given code. The issue I have is,
Once after loading the full page, a empty line is keep on getting inserted in top and bottom of page. So my page is looking like the one given below.
empty line
empty line
empty line
....
pdf content
empty line
empty line
empty line
....
The page is keep on increasing with empty lines.
There was something wrong with Google only, now I am not getting this issue, it works fine.

Categories

Resources