Android create pdf document from webview with multiple pages
Asked Answered
S

2

11

Im using the PdfDocument framework from Android (link) to create a pdf document from my webview content. The pdf is created well but it is only one page document. When the webview content is large i need to create a multipage document. ALL I NEED IS TO SPLIT WEBVIEW CONTENT IN PAGES. How can i achieve this? I dont 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();
Sibyls answered 26/1, 2015 at 16:26 Comment(0)
P
7

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

// 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();
Photogram answered 26/1, 2015 at 21:58 Comment(2)
I know that i can add more pages, but what i want is to divide the webview content in multiple pages if that content is too long for an ISO A4 page.Sibyls
ok, I get your problem for long content. I don't see how you'd truncate and spread the content segments over multiple pages. Maybe you can get the height of your webview and scale your canvas ?Photogram
T
2

I was having this same issue for the last couple days, so I found this answer from Rakesh Gopathi, it worked flawlessly. I really recommend anyone who's using the native PdfDocument classes to check it out.

Tarnetgaronne answered 12/7, 2017 at 13:0 Comment(2)
i checked it; it works fine but still i cant create multiple pages in that, only single page creates.Benumb
It worked as expected for me. Multiple pages were createdAcclaim

© 2022 - 2024 — McMap. All rights reserved.