wkhtmltopdf重复thead标题重叠内容

Jer*_* I. 24 wkhtmltopdf

我们在一个Java应用程序中嵌入了wkhtmltopdf(0.12.1),使用stdin和stdout进行输入/输出.我们想要在PDF中使用多个(不同的)标题,因此我们不会使用--header-html我们使用的选项thead,而是在多个页面上重复使用.这是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>
Run Code Online (Sandbox Code Playgroud)

到现在为止还挺好.当内容跨越多个页面时出现问题.然后标题显示在内容的顶部,与其重叠.示例htmlPDF.请注意,第二个标题渲染得很好,因为tr它只覆盖一页.

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

Jua*_*lez 39

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

thead { display: table-header-group; }
tfoot { display: table-row-group; }
tr { page-break-inside: avoid; }
Run Code Online (Sandbox Code Playgroud)

  • 我们有一个复杂的多行标题,带有多个列跨度和行跨度,这个解决方案非常适合我们。 (2认同)

Tun*_*zle 8

你通过添加以下css来解决这个问题.

   tr {
         page-break-inside: avoid;
      }
Run Code Online (Sandbox Code Playgroud)


Hug*_*ugh 6

我发现tr { page-break-inside: avoid; }工作到了一定程度,但当我的标题跨越多行时就不行了。由于我想保留我的thead部分,我的解决方案是关闭重复。

thead, tfoot {
  display: table-row-group;
}
Run Code Online (Sandbox Code Playgroud)

  • 这导致theader没有打印在以下页面上(wkhtmltopdf 0.12.5-1.msvc2015-win32) (2认同)