如何在 HTML 中插入分页符,以便 wkhtmltopdf 解析它?

因此,基本上我使用 wkhtmltopdf 将动态 HTML 报告转换为 PDF。

报告有一个特定的格式,我被要求用 HTML 克隆该格式。

我面临的问题是,我不能在 html 中模拟100% 的功能性页面分割,因此 wkhtmltopdf 可以解释它并将内容发送到另一个页面。

我试图避免页面大小测量方法等。

提供任何帮助。

编辑: 到目前为止,我正在使用 <br>模拟分页,并且它工作正常。但是我仍然需要做一些测试。

83951 次浏览

Specify a CSS rule specific to the print media type. There are a number of properties that can be used for paging. I find it easiest to attach the page-break-before property to a class, as shown below.

In the HTML:

<p>Page 1, paragraph 1</p>
<p class="new-page">Page 2, paragraph 1</p>
<p>Page 2, paragraph 2</p>

In the CSS:

@media print {
.new-page {
page-break-before: always;
}
}

When I use wkhtmltopdf, I work with the following classes:

.keep-together {
page-break-inside: avoid;
}


.break-before {
page-break-before: always;
}


.break-after {
page-break-after: always;
}

So I can force:

  • That a particular container content is not spread over two pages (if it fits one page). When using the keep-together class a page break is created before the container if necessary.
  • That a page break is forced before a particular container.
  • That a page break is forced after a particular container.

The @media print didn't work for me with wkhtmltopdf.

<div style = "display:block; clear:both; page-break-after:always;"></div>

Just add that in your html code

people having this issue, try downgrading wkhtmltopdf to version 0.10.0 rc2, worked for me

@media print worked for me with wkhtmltopdf when I used the option --print-media-type for wkhtmltopdf