Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I force a page break in HTML printing?

I'm making a HTML report that is going to be printable, and it has "sections" that should start in a new page.

Is there any way to put something in the HTML/CSS that will signal to the browser that it needs to force a page break (start a new page) at that point?

I don't need this to work in every browser out there, I think I can tell people to use a specific set of browsers in order to print this.

like image 485
Daniel Magliola Avatar asked Oct 09 '22 02:10

Daniel Magliola


People also ask

How do you force a page-break in HTML?

It's done via CSS (inline or in a stylesheet) by adding 'page-break-after: always' to a paragraph at the bottom of your page (above where you intend to break). Optionally, you may need to also add 'page-break-before:always' to the paragraph or heading (or other block level element) at the top of the next page.

How do you put a page-break after an HTML element in CSS?

The page-break-after property adds a page-break after a specified element. Tip: The properties: page-break-before, page-break-after and page-break-inside help to define how a document should behave when printed. Note: You cannot use this property on an empty <div> or on absolutely positioned elements.

How do I print an entire page in HTML?

window. print() WILL print the whole page. I am using microsoft on note ,after selecting the "all page" ,result is still same .

How do I force a website to print?

Press the "Print Screen" key if the Web page that you want to print fits within a single browser window and does not require scrolling. This will capture a screen shot of the browser window and copy the image to your clipboard.


4 Answers

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;
    }
}
like image 505
Chris Doggett Avatar answered Oct 19 '22 10:10

Chris Doggett


Try this link

<style>
@media print
{
h1 {page-break-before:always}
}
</style>
like image 53
jfarrell Avatar answered Oct 19 '22 12:10

jfarrell


First page (scroll down to see the second page)
<div style="break-after:page"></div>
Second page
<br>
<br>
<button onclick="window.print();return false;" />Print (to see the result) </button>

Just add this where you need the page to go to the next one (the text "page 1" will be on page 1 and the text "page 2" will be on the second page).

Page 1
<div style='break-after:always'></div>
Page 2

This works too:

First page (there is a break after this)
<div style="break-after:page"></div>
Second page (This will be printed in the second page)
like image 34
PythonProgrammi Avatar answered Oct 19 '22 12:10

PythonProgrammi


Just wanted to put an update. page-break-after is a legacy property now.

Official page states

This property has been replaced by the break-after property.

like image 20
shazyriver Avatar answered Oct 19 '22 12:10

shazyriver