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
However I am not able to get it right.
The amount of pages created in the PDF is fine, but the content is not.
A PDF with (in this case) 3 similar pages is created. All showing the webview of before the method was called.
Once finished creating the PDF, the last url of the url array loads.
How can i achieve that a pdf page is created only after a new url is loaded in webview?
I did try to use delay, but that does not seem to be the issue.
If delayed, the pdf is created first and pages are opened up only after.
I did try to call the pdf creation from within onPageFinished().
It did not do the job either.
Maybe i did use all this wrong.
Here is what my code looks like right now:
//Create folder
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String dirName = "exampledirectory";
File newdir = new File(baseDir + File.separator + dirName);
newdir.mkdirs();
//Create PDF
String fileName = "example.pdf";
String fileNameWithPath = newdir + File.separator + fileName;
//Create document
PdfDocument document = new PdfDocument();
String[] urlArr = {"exampleurl1.com", "exampleurl2.com", "exampleurl3.com"};
for (int i = 0; i < urlArr.length; i++) {
mWebView.loadUrl(urlArr[i]);
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(mWebView.getMeasuredWidth(), mWebView.getContentHeight(), i).create();
// start [i]st page
PdfDocument.Page page = document.startPage(pageInfo);
// draw on the page
View content = mWebView;
content.draw(page.getCanvas());
// finish [i]st page
document.finishPage(page);
}
FileOutputStream fos;
try {
fos = new FileOutputStream(fileNameWithPath, false);
// write the document content
document.writeTo(fos);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
document.close();
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…