Android - Problem in using the Google pdf viewer in webview - android

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.

Related

All webview content printed in one page

Case: User should be able to view and print pdf
My solution: I am opening PDF inside Webview with the help of docs.google.com/gview. Below is my code
Set up Webview
string url = "http://www.africau.edu/images/default/sample.pdf";
string gview = $"https://docs.google.com/gview?embedded=true&url={url}";
mWebView.LoadUrl(gview);
Print PDF
var printMgr = (PrintManager)GetSystemService(PrintService);
printMgr.Print("print", mWebView.CreatePrintDocumentAdapter("print"), null);
Below is the screenshot. As you can see PDF loads just fine
Problem
When I want to print PDF, all the PDF pages are printed in one paper which you can see below
I would appreciate any suggestion, including different library for displaying/printing pdf or suggestion in Java or Kotlin, I can convert them to C#.
I would not print the web page but print the PDF directly as when printing the web page it just sees it as a longer web page and knows nothing about the content.
Use a custom print adapter instead, but instead of drawing a PDF to print you can just use the existing PDF you already have.
See for details https://developer.android.com/training/printing/custom-docs.html

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

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"");

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

android webview not showing html tabs properly

In my app , I am having a webview . In webview ,I am showing HTML content which is having tag , .I have 2 tabs in that which should switch on clicking on same place. But in webview they are coming one after the other.
In HTML file, I am calling method of Jquery file that method is not getting called . In browser its working fine. In iOS its working.
I am loading webview with base Url method only.
webView.loadDataWithBaseURL(pathToHtml, strLoad, "text/html","UTF-8", "");
These files are in assets . I am not sure that I am able to provide correct path for jquery files. So , I copied files to sdcard. There I am giving path. So path can not be the issue.
Please help me out . I tried many things but nothing worked. I am not getting what is the issue.
You can call a javascript function from webView.loadUrl("javascript:testFunction();"); to change the tabs insted of loading the page again. at the first load, you can use webView.loadUrl("file:///android_asset/index.html"); Befor using the jquery code, make sure that javascript enabled, set the webviewclient and webchromeclient for the webView.

page not available when opening link in webview

I take the response from an HTTP connection in the form of string and show that to webview like this:
WebView engine = (WebView)findViewById(R.id.webview);
engine.loadData(endResult, "text/html", "UTF-8"); /*endresult is string*/
I actually get a response that contains the google page (google search result direct from google.com).
The loadData method works well i.e it shows the web page but when I click on one of the links on that page it shows "page not available" and said that "xyz link might be temporarily down or it may have moved to permanently to a new web address".
this happens for all links accept the first present link on that page. i.e it shows correct page from first link on that page but fails for others..
I noticed that OSes prior to 2.3 failed to follow links if setHorizontalScrollBarEnabled and setVerticalScrollBarEnabled are set to false.
try to use loadDataWithBaseURL of the WebView class
I would avoid using engine.loadData - it seems to cause all sorts of crazy problems.
Use engine.loadDataWithBaseURL instead, and pass the base URL of where the content exists. I would think that the content you are loading is using relative paths in it's HTML so it's looking inside your app resources. By specifying the base URL you get around this problem.

Categories

Resources