How to Force a Page Break in HTML Printing

Can I force a page break in HTML printing?

Add a CSS class called "pagebreak" (or "pb"), like so:

@media print {
.pagebreak { page-break-before: always; } /* page-break-after works, as well */
}

Then add an empty DIV tag (or any block element that generates a box) where you want the page break.

<div class="pagebreak"> </div>

It won't show up on the page, but will break up the page when printing.

P.S. Perhaps this only applies when using -after (and also what else you might be doing with other <div>s on the page), but I found that I had to augment the CSS class as follows:

@media print {
.pagebreak {
clear: both;
page-break-after: always;
}
}

Forcing a page break only when necessary during print

Terns out that embedding divs are not supported for page-break-inside:avoid. In order to accomplish what I was trying to do, I needed to convert my html to a table and surround it with a single div. This also works on FF(38.0.5), IE(11), and Chrome(43.0.2357.130 m)

<div style="page-break-inside: avoid;">
<table style="width:100%;">
<tbody>
<tr>
<td style="width:400px;">
<h2>**Delinquency Notice** as of 12/31/13</h2>
</td>
<td style="width:400px;">
<h2>Recent Account History</h2>
</td>
</tr>
<tr>
<td class="padding"><b>You are late on your mortgage payments.</b> 
As of 4/17/2015, you are 106 days late on your mortgage payments. 
Failure to bring your loan current may result in fees and 
foreclosure -- the loss of your home.<br/><br/>If you are experiencing financial difficulty, HUD
(U.S. Dept 
of Housing and Urban Development) approved counseling agents 
are available to provide mortgage and foreclosure counseling 
assistance. Refer to the HUD disclosure section on the 
statement front for contact information.
</td>
<td class="padding">
<ul style="list-style-type: disc;">
<li>Payment due 12/01/2014: Fully paid on 11/30/2014</li>
<li>Payment due 1/01/2015: Unpaid balance of $1,290.02</li>
<li>Payment due 2/01/2015: Fully paid on 1/31/2015</li>
<li>Payment due 3/01/2015: Fully paid on 2/28/2015</li>
<li>Payment due 4/01/2015: Fully paid on 3/17/2015</li>
<li>Current payment due 5/01/2015: $1,290.02</li>
</ul>
<br/><b>Total: $6,495.86 due. You must pay this amount to bring your loan current</b></td>
</tr>
</tbody>
</table>
</div>

How to define page breaks when printing html?

You can add page breaks for printing with a little bit of CSS.

CSS:

@media all {
.page-break { display: none; }
}

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

HTML:
Use a div element with the page-break class where you want insert your breaks

<div class="page-break"></div>

Example:

<div>Some content BEFORE the page break</div>
<div class="page-break"></div>
<div>Some content AFTER the page break</div>
<div class="page-break"></div>
<div> ... More content after another page break ... </div>

CSS page-break-after to have even pages in Chrome

You definitely should keep in mind, that page-break properties are not supported correctly in most browsers. Check the caniuse report.

Also, here is the question that might help you, it already has some good answers.

Duplicating the main suggestions here with my own commentaries. It may be, that you can use the mix of every of them.

Number 1. (@Nils)

You could maybe try wrapping each of your reports in a div and then using something like jQuery to work out the height of the div to figure out whether it ends on an odd page.

If it ends on an odd page, then inject an empty div with your page-break-after class after that so that it feeds to the next page.

Obviously this will only really work if you know the dpi at which your page prints on your target printer. There's no magic answer that will just work for all scenarios.

  • 72 dpi (web) = 595 X 842 pixels
  • 300 dpi (print) = 2480 X 3508 pixels
    ("210mm X 297mm @ 300 dpi")
  • 600 dpi (print) = 4960 X 7016 pixels

You'd need to experiment a bit here with your standard printer settings to see what works for you/your client(s). If there are multiple scenarios, you could let them select from a drop-down.

So you'd use jquery to check the pixel height of the div, check it against the pixel height of the page to see if the div ends on an odd or even page - then inject the page break if the report ends on an odd page.

You'd also need to know upfront if the user will be using duplex printing - because you'd only need to do this for duplex printing.

Number 2. (@spekary)

Use inline styles for manual page-breaking where it is needed.

<div style="page-break-after: right">
<!-- your content -->
</div>

However, you should keep in mind that this doesn't exactly solve your problem, because, as you said, the document

will contain unspecified number of text elements of unspecified length.

But still, maybe you can play around with this solution.

How to deal with page breaks when printing a large HTML table

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test</title>
<style type="text/css">
table { page-break-inside:auto }
tr { page-break-inside:avoid; page-break-after:auto }
thead { display:table-header-group }
tfoot { display:table-footer-group }
</style>
</head>
<body>
<table>
<thead>
<tr><th>heading</th></tr>
</thead>
<tfoot>
<tr><td>notes</td></tr>
</tfoot>
<tbody>
<tr>
<td>x</td>
</tr>
<tr>
<td>x</td>
</tr>
<!-- 500 more rows -->
<tr>
<td>x</td>
</tr>
</tbody>
</table>
</body>
</html>


Related Topics



Leave a reply



Submit