Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making paper-aligned html to be used with WKHTMLTOPDF

I'm trying to implement a report engine using WKHTMLTOPDF and I'm having trouble aligning the content with paper edges.

Consider I'm setting the paper size to A4 and print margins to zero like this:

wkhtmltopdf-amd64 --load-error-handling ignore --orientation Portrait --page-size A4 --margin-top 0 --margin-bottom 0 --margin-left 0 --margin-right 0

Now I would like to make a report in which the content is not cut off by paper edges. e.g. my content is consisted of tables and cells and I don't want a cell to be splitted into two pages. CSS2 has a feature specifically for this, page-break-inside:avoid, but I know no browser that supports it, and webkit is no exception.

Then I thought if I knew how long actually is an A4 paper in WKHTMLTOPDF then I can write some Javascript to align rows by resizing them. Since there was no mean to tell the actual millimeters within CSS to fill an A4 paper completely, I had no choice but to use error and trial. Finally I managed to tune the css height (height: 290.245261mm) so the page content fills up an A4 paper vertically. This height is set for a DIV with absolute positioning and top and left set to zero. All the effective paddings and margins are also set to zero (HTML, BODY, ...).

All was fine until I realized the severity of situation when number of pages grows. So I put ten of full-page DIVs within my HTML and to my surprise it printed 11 pages. With one tiny row of pixels at the beginning of the 11th page.

So, here's my question: Has anyone come up with a solution to align HTML content with paper edges when converting an HTML using WKHTMLTOPDF? And I'm using WKHTMLTOPDF 0.11.0 rc1.

like image 444
Mehran Avatar asked Dec 08 '13 12:12

Mehran


1 Answers

(Though this is an old question, people still ask so...)

The best fix is to update your wkhtmltopdf tool.

The bug you describe was fixed in version 0.12 and table rows are no longer split into two pages ( as mentioned in the linked changelog:

  • [qt] prevent page breaks in table rows

I am using the same library for multiple projects and this saved me, even if I had to custom compile it back then.

like image 158
Nick Andriopoulos Avatar answered Sep 18 '22 12:09

Nick Andriopoulos