Open Several Pages In Webview And Create Pdf
I am trying to build an app to create a PDF from several pages in webview. As beginner i was happy i found this: Android create pdf document from webview with multiple pages Howeve
Solution 1:
I found a solution that works for me. It feels a bit odd, but it does the job.
As I mentioned above onPageFinished() did not work on my first tries, cause the page is not rendered yet when called. Now I tried again calling another method from onPageFinished() which then calls the PDFbuilder Methods. With this "workaround" I was able to build a PDF from all URLs. Here`s the code:
privateclassprintPDFFromWebcontent {
int i = 0;
publicString pageURL = "exampleURL";
publicPdfDocumentdocument = newPdfDocument();
publicString fileNameWithPath ="blank.pdf";
privatevoidrunPDFBuilder() {
zWebView = (WebView) findViewById(R.id.activity_main_webview);
WebSettings webSettings = zWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
zWebView.setWebViewClient(newWebViewClient() {
@OverridepublicvoidonPageFinished(WebView view, String url) {
super.onPageFinished(zWebView, url);
i++;
openNextPage();
}
privatevoidopenNextPage() {
String nextPage = pageURL + "?page=" + String.valueOf(i + 1); //This works with my specific target URLs. You may want to use an URL array.if (i == 1) {
zWebView.loadUrl(nextPage);
createPDFNow();
}
if (i == 2) {
zWebView.loadUrl(nextPage);
addPageToPDF();
}
if (i == 3) {
zWebView.loadUrl(nextPage);
addPageToPDF();
}
if (i == 4) {
mWebView.loadUrl("whateverYouWantToShowIfFinishedURL");
//Not added to the PDF//Reason is to trigger onPageFinished() once more. You could probably i++ here and call openNextPage() from within.addPageToPDF(); //Add the last webview content to PDF.
}
if (i == 5) {
createPDFNow();
Toast.makeText(getApplicationContext(), "Finito.", Toast.LENGTH_SHORT).show();
savePDF();
return;
}
}
publicvoidcreatePDFNow(){
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String dirName = "example";
File newdir = newFile(baseDir + File.separator + dirName);
newdir.mkdirs();
String fileName = "example.pdf";
fileNameWithPath = newdir + File.separator + fileName;
}
publicvoidaddPageToPDF() {
PdfDocument.PageInfo pageInfo = newPdfDocument.PageInfo.Builder(zWebView.getMeasuredWidth(), zWebView.getContentHeight(), i).create();
PdfDocument.Page page = document.startPage(pageInfo);
View content = zWebView;
content.draw(page.getCanvas());
document.finishPage(page);
}
publicvoidsavePDF(){
FileOutputStream fos;
try {
fos = newFileOutputStream(fileNameWithPath, false);
document.writeTo(fos);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
document.close();
}
});
zWebView.getSettings().setUseWideViewPort(true);
zWebView.getSettings().setLoadWithOverviewMode(true);
zWebView.loadUrl("URL1");
return;
}
However if there is a smarter solution: I`d like to learn.
Post a Comment for "Open Several Pages In Webview And Create Pdf"