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

HtmlPdfWkhtmltopdfPage Break

Html Problem Overview


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.

Html Solutions


Solution 1 - Html

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

Solution 2 - Html

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 .

Solution 3 - Html

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

Just add that in your html code

Solution 4 - Html

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

Solution 5 - Html

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlenordView Question on Stackoverflow
Solution 1 - HtmlGeraint AndersonView Answer on Stackoverflow
Solution 2 - HtmlJuangui JordánView Answer on Stackoverflow
Solution 3 - HtmlEzekielView Answer on Stackoverflow
Solution 4 - HtmltommikkoView Answer on Stackoverflow
Solution 5 - HtmlLucas Souza TeixeiraView Answer on Stackoverflow