Recently, I came across a request to put a mobile website into an app, so people won't need to navigate the browser to access the pages.
WebView is a very nice API to serve this purpose, and this doesn't need to invoke the original mobile website design and coding.
The first challenging thing is I need to successfully download the PDF files that are generated by TCPDF in run time. Since WebView does not handle this kind of request automatically like the typically desktop browsers do, such as chrome or firefox etc. I got to adding code to WebVeiw how to handle the PDF which is generated by TCPDF.
I found that there were not even a Q&A like stackoverflow mentioned WebView with TCPDF. Anyway, there were still some Q&A about WebView and PDF/file download. But, most of the examples and questions are about downloading a physical file on server which is not exactly the same as working with TCPDF. But those materials are still helpful to me, a newbie of Android, a lot to figure out a solution.
So, now I want to share how this can be achieved, even the code may be raw, but this works for me.
First, initialing the download in setDownloadListener in onCreate Activity, after WebView instant is created, like the following:
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());
myWebView.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
//start download
DownloadPDF downloadPDF = new DownloadPDF();
downloadPDF.execute(url,userAgent,contentDisposition);
}
});
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("http://www.examlple.com");
And then, adding a private class DownloadPDF() which extends Asynctask like below:
private class
DownloadPDF extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... sUrl) {
try {
URL url = new URL(sUrl[0]);
File myDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS).toString()+"/myPDF");
// create the directory if it does not exist
if (!myDir.exists()) myDir.mkdirs();
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.connect();
//get filename from the contentDisposition
String filename = null;
Pattern p = Pattern.compile("\"([^\"]*)\"");
Matcher m = p.matcher(sUrl[2]);
while (m.find()) {
filename = m.group(1);
}
File outputFile = new File(myDir, filename);
InputStream input = new BufferedInputStream(connection.getInputStream());
OutputStream output = new FileOutputStream(outputFile);
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
output.write(data, 0, count);
}
connection.disconnect();
output.flush();
output.close();
input.close();
//
displayPdf(); a function to open the PDF file automatically
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
Lastly, remember to add permission in Mainifest.xml:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />.
Done. The next thing to do is "Open the PDF file automatically".