Home > Enterprise >  View PDF file in localhost using webview android
View PDF file in localhost using webview android

Time:01-11

I have url of pdf file in my localhost. how i can load the pdf in webview android application.

String pdf = "my localhost pdf url"
WebView webView = findViewById(R.id.web);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setSupportZoom(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://docs.google.com/gview?embedded=true&url="  pdf);

this method is only for pdf file that stored in server.

Any methods to show pdf(stored in localhost) in webview ? with or without using third party libraries.

CodePudding user response:

You can't that way. You're using Google to download the file and display it. That's not going to ever work, because a server can't download a file from your device- your device isn't an http server. You're going to either need to use an actual pdf viewing library to display the pdf, or you may be able to make it work by uploading the file to their google drive then displaying it from there- although that way has a LOT of drawbacks (such as storing stuff the user doesn't want on drive. And assuming they have a drive account in the first place). Realistically, you're probably going to need to go the pdf viewing library route.

CodePudding user response:

Download ngrok (https://ngrok.com/download). Launch it (Make sure that your machine has internet connection). Copy the https address that's generated. Paste the address in the WebView.loadUrl. Append the path to your pdf on your localhost (Don't include "localhost"!). Run your app.

  •  Tags:  
  • Related