Click or drag to resize

Automatic and Custom Page Breaks, Keep Together

The Chromium engine paginates the HTML the way the Chrome browser does when it prints a page, and it honors the CSS properties that control the page breaks:

  • page-break-before: always (or break-before: page)

  • page-break-after: always (or break-after: page)

  • page-break-inside: avoid (or break-inside: avoid)

For example, with the page-break-after: always style applied to an HTML element (an image, a paragraph, a section) you instruct the converter to start a new page right after that element.

A line of text is never cut between two pages: the engine moves it to the next page. An image or a block can be split across a page boundary; give it the page-break-inside: avoid style to keep it whole on one page.

The page-break-inside: avoid style is also how you keep a group of HTML elements together on the same page - a table row, a table, a div with a heading and its paragraph. This is very useful for reports. In the example below the table contains a large number of rows, each with an image on the left and a text on the right, and no row is split between two pages.

XML
<table>
    <tr style="page-break-inside: avoid">
        <td>
            <img width="100" height="100" src="img1.jpg">
        </td>
        <td>
            My text 1
        </td>
    </tr>

    <tr style="page-break-inside: avoid">
        <td>
            <img width="100" height="100" src="img2.jpg">
        </td>
        <td>
            My text 2
        </td>
    </tr>
</table>

The styles can be in the page itself or added at conversion time with CustomCSS, for example to start every chapter of a page you cannot change on a new page:

PdfConverter pdfConverter = new PdfConverter();
pdfConverter.CustomCSS =
    "h1 { page-break-before: always; } img { break-inside: avoid; }";
pdfConverter.SavePdfFromUrlToFile("https://www.example.com/book", "book.pdf");
See Also

Other Resources

Repeat Table Header