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

lenord picture lenord · Feb 2, 2017 · Viewed 47.6k times · Source

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

The report has a particular format and I've been asked to clone that format with 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.

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

TIA for any help provided.

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

Answer

Geraint Anderson picture Geraint Anderson · May 22, 2017

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;
  }
}