wkhtmltopdf repeating thead headers overlapping content
Asked Answered
C

4

35

We're embedding wkhtmltopdf (0.12.1) in a Java application, using stdin and stdout for input/output. We want multiple (different) headers in our PDF, so instead of using the --header-html option we're using a thead, which is repeated on several pages. Here's a little example HTML:

<!DOCTYPE html>
<html>
<body> 
    <table style="page-break-after: always;">
        <thead>
            <tr>
                <th>My first header</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>First content</td>
            </tr>
        </tbody>
    </table>
    <table>
        <thead>
            <tr>
                <th>My second header</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Second content</td>
            </tr>
        </tbody>
    </table>
</body>
</html>

So far so good. Problems arise when the content spans multiple pages. The header is then displayed on top of the content, overlapping it. Example html and PDF. Notice that the second header is rendered just fine, since the tr only spans one page.

Other people have had similar problems. There are some workarounds for this when you're using the --header-html option, such as adding --header-spacing or --margin-top, but these options have no effect on the repeated thead. Any ideas?

Crandall answered 9/7, 2014 at 8:5 Comment(1)
This is a known problem with wkhtmltopdf, see issue 1524.Dayledaylight
F
65

I solved it with these three css rules:

thead { display: table-header-group; }
tfoot { display: table-row-group; }
tr { page-break-inside: avoid; }
Fertilizer answered 30/12, 2015 at 20:9 Comment(5)
We had a complex multi-line header with muliple colspans and rowspans, and this solution worked perfectly for us.Tnt
thanks a lot!! I had a problem with this bug for a long time, and this solution worked perfectly in my PDF'sArlindaarline
In my test, using the thead OR the tr rule worked. Seems like it just needs a nudge :-)Hennery
This is an older question, but this solution worked for me.Agio
Did the job for meToname
W
16

you solve this issue by adding the following css.

   tr {
         page-break-inside: avoid;
      }
Wartime answered 31/8, 2015 at 9:40 Comment(0)
P
10

I found tr { page-break-inside: avoid; } worked to a point, but not when my headers spanned multiple lines. Since I wanted to keep my thead section, my solution was to switch the repeat off.

thead, tfoot {
  display: table-row-group;
}
Portugal answered 8/1, 2016 at 0:43 Comment(2)
This did the job for me!Ealasaid
This causes that the theader is not printed on the following pages (wkhtmltopdf 0.12.5-1.msvc2015-win32)Rapscallion
D
0

I'm using wkhtmltopdf 0.12.6 (with patched qt), I had the same issue resolved by removing the parent container (e.g. <div>...</div>).

Duro answered 16/1 at 7:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.