Android creates a PDF from a multi-page web browser

Im using the Android PdfDocument framework ( link ) to create a pdf document from my web content. The PDF file is well created, but it is only one page document. When the content of the web pages is large, I need to create a multi-page document. EVERYTHING I NEED TO DISTRIBUTE THE WEBVIEW CONTENT ON THE PAGES. How can I achieve this? I do not want to use iText or any third-party library.

Need help please. Thanks in advance.

// create a new document PdfDocument document = new PdfDocument(); // create a page description PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(width, height, 1).create(); // start a page PdfDocument.Page page = document.startPage(pageInfo); // draw something on the page View content = myWebview; content.draw(page.getCanvas()); // finish the 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(); } // close the document document.close(); 
+6
source share
2 answers

If you want to create multiple pages, just call startPage () and finishPage () for each page that you want to create in your document.
Something like that:

 // create document PdfDocument document = new PdfDocument(); // create a page description PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(width, height, 1).create(); // start 1st page PdfDocument.Page page = document.startPage(pageInfo); // draw something on the page View content = myWebview; content.draw(page.getCanvas()); // finish 1st page document.finishPage(page); // start 2nd page PdfDocument.Page page = document.startPage(pageInfo); // draw something on the page View content = someOtherWebview; content.draw(page.getCanvas()); // finish 2nd page document.finishPage(page); // and so on... 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(); } // close the document document.close(); 
+2
source

I had the same problem in the last couple of days, so I found this answer from Rakesh Gopathi, it worked flawlessly. I really recommend to anyone using the PdfDocument class to check it out.

0
source

Source: https://habr.com/ru/post/981558/


All Articles