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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42005819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 14:20:18  来源:igfitidea点击:

How to insert a page break in HTML so wkhtmltopdf parses it?

htmlpdfwkhtmltopdfpage-break

提问by lenord

So basically I'm using wkhtmltopdf to convert a dynamic HTML report to PDF.

所以基本上我使用 wkhtmltopdf 将动态 HTML 报告转换为 PDF。

The report has a particular format and I've been asked to clone that format with HTML.

该报告具有特定格式,我被要求使用 HTML 克隆该格式。

The problem I'm facing is that I can't simulate a 100% functional page break in html so wkhtmltopdf can interpret it and send content to another page.

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

I'm trying to avoid page size measurement methods and so.

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

TIA for any help provided.

TIA 提供的任何帮助。

EDIT: So far I'm emulating page break using <br>and it works. I still have to do some testing though.

编辑:到目前为止,我正在模拟分页符使用<br>并且它有效。不过,我仍然需要做一些测试。

回答by Geraint Anderson

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-beforeproperty to a class, as shown below.

指定特定于打印介质类型的 CSS 规则。有许多属性可用于分页。我发现将page-break-before属性附加到类最容易,如下所示。

In the HTML:

在 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:

在 CSS 中:

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

回答by Juangui Jordán

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

当我使用 时wkhtmltopdf,我使用以下类:

.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-togetherclass 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.
  • 特定容器内容不会分布在两页上(如果它适合一页)。使用keep-together该类时,如有必要,会在容器之前创建一个分页符。
  • 在特定容器之前强制分页。
  • 在特定容器之后强制分页符。

The @media printdidn't work for me with wkhtmltopdf.

@media print没有为我工作wkhtmltopdf

回答by Ezekiel

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

Just add that in your html code

只需将其添加到您的 html 代码中