看起来wkhtmltopdf在处理表格时对分页符有点过敏,因为在SO中不同版本的问题和答案的数量不同。
在撰写本文时,我正在运行最新版本0.12.6。
这是我的html代码:
<HTML>
<head>
<title>A nice page</title>
<link rel="stylesheet" href="screen.css" media="screen" />
<link rel="stylesheet" href="printer.css" media="print" />
</head>
<body>
<table class="printer_friendly">
<tbody class="printer_friendly_tbody">
<tr>
<td class="tbody_first" colspan="6">This is the beginning of a new picture. This must be the first in the groupof tbody 0</td>
</tr>
<tr>
<th colspan="3.0" rowspan="5">
<img src="images/pdf_download.png" width="300px" />
</th>
<td>Random text</td>
<td>Random text</td>
<td>Random text</td>
</tr>
<tr>
<td>This is (0, 0) in tbody 0</td>
<td>This is (0, 1) in tbody 0</td>
<td>This is (0, 2) in tbody 0</td>
</tr>
<tr>
<td>This is (1, 0) in tbody 0</td>
<td>This is (1, 1) in tbody 0</td>
<td>This is (1, 2) in tbody 0</td>
</tr>
<tr>
<td>This is (2, 0) in tbody 0</td>
<td>This is (2, 1) in tbody 0</td>
<td>This is (2, 2) in tbody 0</td>
</tr>
<tr>
<td>This is (3, 0) in tbody 0</td>
<td>This is (3, 1) in tbody 0</td>
<td>This is (3, 2) in tbody 0</td>
</tr>
<tr>
<td colspan="6">This is (0, 2) in tbody 0</td>
</tr>
<tr>
<td colspan="6">This is (1, 2) in tbody 0</td>
</tr>
</tbody>
<tbody class="printer_friendly_tbody">
<tr>
<td class="tbody_first" colspan="6">This is the beginning of a new picture. This must be the first in the groupof tbody 1</td>
</tr>
<tr>
<th colspan="3.0" rowspan="5">
<img src="images/pdf_download.png" width="300px" />
</th>
<td>Random text</td>
<td>Random text</td>
<td>Random text</td>
</tr>
<tr>
<td>This is (0, 0) in tbody 1</td>
<td>This is (0, 1) in tbody 1</td>
<td>This is (0, 2) in tbody 1</td>
</tr>
<tr>
<td>This is (1, 0) in tbody 1</td>
<td>This is (1, 1) in tbody 1</td>
<td>This is (1, 2) in tbody 1</td>
</tr>
<tr>
<td>This is (2, 0) in tbody 1</td>
<td>This is (2, 1) in tbody 1</td>
<td>This is (2, 2) in tbody 1</td>
</tr>
<tr>
<td>This is (3, 0) in tbody 1</td>
<td>This is (3, 1) in tbody 1</td>
<td>This is (3, 2) in tbody 1</td>
</tr>
<tr>
<td colspan="6">This is (0, 2) in tbody 1</td>
</tr>
<tr>
<td colspan="6">This is (1, 2) in tbody 1</td>
</tr>
</tbody>
</table>
</body>
</html>
根据SO的建议,我对tbody中应该放在一起的行进行了分组,并将打印介质的tbody属性设置为:
.printer_friendly_tbody {
background-color:aliceblue;
page-break-inside:avoid;
page-break-before:auto;
page-break-after:auto;
}
我想把tbody行放在一起,如果需要,在. printer_friendly_tbody之前或之后拆分表。
当我使用Chrome的Print/PDF功能时,这是生成一个完美的PDF,但当使用wkhtmltopdf生成PDF时,它失败了。
我已经尝试了启用和禁用打印介质选项。
你可能会奇怪,为什么我坚持使用wkhtmltopdf,原因是我想使用wkhtmltopdf的其他功能。
你有什么建议吗?
1条答案
按热度按时间kxeu7u2r1#
我可能已经找到了一个解决方案,或者如果它不是真正的解决方案,它至少是一个变通方案。
我已经创建了一个gist,下面的代码片段。
它正在使用wkhtmltopdf和Chrome的PDF打印选项在所需位置生成带有分页符的PDF。
我希望它能对某人有所帮助!