wkhtmltopdf 重复标题重叠内容

问题描述:

我们将 wkhtmltopdf (0.12.1) 嵌入到 Java 应用程序中,使用 stdin 和 stdout 进行输入/输出.我们希望在我们的 PDF 中有多个(不同的)标题,因此我们不使用 --header-html 选项,而是使用 thead,它在多个页面上重复.下面是一个 HTML 的小例子:

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>

到目前为止一切顺利.当内容跨越多个页面时会出现问题.标题然后显示在内容的顶部,重叠它.示例 htmlPDF.请注意,第二个标题呈现得很好,因为 tr 只跨越一页.

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.

其他人也遇到过类似的问题.当您使用 --header-html 选项时,有一些解决方法,例如添加 --header-spacing--margin-top,但这些选项对重复的 thead 没有影响.有什么想法吗?

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?

我用这三个css规则解决了:

I solved it with these three css rules:

thead { display: table-header-group; }
tfoot { display: table-row-group; }
tr { page-break-inside: avoid; }