I am using web-view in my native android application. But the issue I am facing is that I have a PHP file generatePDF.php which takes sessions, parameters from URL and output pdf file on that URL. The issue is that when I pass that URL to webview directly it doesn't load pdf in webview but instead if I pass direct pdf file link through anchor link it loads.
WebView wv = findViewById(R.id.wvBrowser);
WebSettings ws = wv.getSettings();
wv.loadUrl("http://localhost/generatePDF.php?uid=1");
Above code don't load pdf. But if i add some anchor tag and add above path in href then it opens that pdf.
wv.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=http://localhost/generatePDF.php?uid=1");
I tried like above and it loads pdf directly but it doesn't pass the sessions.
Kindly guide how can I load my generatePDF file directly in web-view
WebView has no ability to display PDFs. Either:
Use something like PDF.js to be able to parse and render PDF in a WebView, or
Use a library to render a PDF as its own view
See this blog post for more on this subject.
Related
I'm using Android Studio's Webview to convert my web app to mobile app.
my webapp has a login which shows your account's files (pdf files).
but running my default app on Android Studio, won't open these pdf files.
I read here to use Google viewer and just put the link of your PDF file just like below,
WebView webview=new WebView(this);
webview.getSettings().setJavaScriptEnabled(true);
String pdf = "http://www.adobe.com/devnet/acrobat/pdfs/pdf_open_parameters.pdf";
webview.loadUrl("http://docs.google.com/gview?embedded=true&url=" + pdf);
however, the webapp is setup wherein if you open the PDF file, it doesn't have the .pdf extension. Example is: https://mysite/files/myfiles/1234567_00/. As you see, the link does not point to a direct pdf file (.pdf extension), instead it only had "/1234567_00/". When I try to replace the link above with the URL (without .pdf extension), it won't open.
Is there a way I can get this to work? What should I change here?
I am aware that this question has been asked many times, but I still cannot get it to work. I have a pdf in the raw folder and I am trying to open it in a webview using "https://drive.google.com/viewerng/viewer?embedded=true&url=" to open it in the webview.
I think my problem is I can't get the correct path for the pdf file. I've tried it three different ways, and either the webview says I get an error (the url doesn't exist), or it "opens" the pdf, but the webview displays this text in the middle, "No preview available".
These are the three ways I've tried to get the path of the file.
"android.resource://com.an.example/raw/filename"
"android.resource://" + getPackageName() + "/" + R.raw.filename;
Environment.getExternalStorageDirectory() + "/filename.pdf"
For methods 1 and 3 I have tried "filename" and "filename.pdf"
None of these are allowing the pdf to load in the webview. Below is the code I use to load the pdf. (Note: this method does work if I use a web url and not a local file)
webview = (WebView) findViewById(R.id.webView);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setBuiltInZoomControls(true);
String pdf = "one_of_the_above_methods";
webview.loadUrl("https://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);
What am I doing wrong? Is there a better way to load a local pdf inside my app without having it take up the entire activity page?
I have a pdf in the raw folder and I am trying to open it in a webview using "https://drive.google.com/viewerng/viewer?embedded=true&url=" to open it in the webview.
That has never been possible.
Is there a better way to load a local pdf inside my app without having it take up the entire activity page?
The best thing is to not load it inside your app at all. Use the user's preferred PDF viewing app, via ACTION_VIEW.
Beyond that, you could use a slightly modified version of PDF.js on Android 4.4+. Or, use AndroidPdfViewer on a wider range of Android versions, though it makes your APK a lot bigger. See this blog post for more context on these and other (worse) options.
Also, here are sample apps demonstrating:
PDF.js: https://github.com/commonsguy/cw-omnibus/tree/v8.10/PDF/PdfJS
AndroidPdfViewer: https://github.com/commonsguy/cw-omnibus/tree/v8.10/PDF/Pdfium
how to convert android web view in PDF in android programming and save as pdf file
if you ask about convert web page to pdf from android you need make back end .
Ex. you have web page view inside android web view. in this status you need make other file in your server to convert html 2 pdf (you can use HTML2PDF library ) to make that. after that you make button in your project in android to query the html file . in this way you will download the html and convert to pdf in you mobile . i wish help you
I have already created the app, but it needs to be connected to the internet to work. In essence it just displays the website through an app. I would like to have the website installed on the app so that it can load without requiring an internet connection.
I have downloaded my webpage while, and added it to the Android SDK. Its saved it under the Java section. Now the code I used to load the webpage while online was the following:
mWebView = (WebView) findViewById(R.id.activity_main_webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("http://kuglerdesign.com/");
mWebView.setWebViewClient(new MyAppWebViewClient());
I have searched on stackoverflow for a similar question, and have found the following lines of code:
URL url = this.getClass().getResource("/com/path/to/file.txt")
File file = new File(url.toURI());
and these from a different answer:
File currFile = new File(getClass().getClassLoader().getResource("the_file.txt").getFile());
My question is how to implement these to a file thats in the Android SDK. Do I just replace all the mWebView and WebSettings or do I still need them as its a HTML file?
Or would I be better off just to set the mWebView Url to a local file?
So replacing the following line of code:
mWebView.loadUrl("http://kuglerdesign.com/");
with this line :
mWebView.loadUrl("file:///kuglerdesign.html");
Any help would be greatly appreciated.
(sorry for being such a novice at Java :( ...)
You have to put your html-files into the assets directory.
d.android.com states for the assets folder:
You can use it to store raw asset files. Files that you save here are compiled into an .apk file as-is, and the original filename is preserved. You can navigate this directory in the same way as a typical file system using URIs and read files as a stream of bytes using the AssetManager. For example, this is a good location for textures and game data.
The WebView can directly load your files, by using file:///android_asset/ as URI path, which can be read here.
That would make your call as easy as mWebView.loadUrl("file:///android_asset/kuglerdesign.html");
I am developing a Java Web Application (jsp/servlet) using tomcat. I need to display pdf file from local machine. can you suggest what is best way to display it?
I used iframe to display pdf file.
<iframe src="resume.pdf" width="100%" style="height:60em">
[Your browser does <em>not</em> support <code>iframe</code>,
or has been configured not to display inline frames.
You can access the document
via a link though.]
</iframe>
I think you can try a Library called XPDF , I think you can convert from PDF to HTML page , or the second option is just let the user open a link to the page (www.yourwebsite.com/pdffolder/somepdf.pdf)
If you need display a pdf file using tomcat, you can access directly to the file using the specific url where the file is located in your navigator, depending on the path where you put the file, so you can access using 127.0.0.1/files/test.pdf for example. If you need generate a pdf, the best tool I think is iText, this is an easy example how to use id: Introducing PDF and iText