How to apply CSS page-break to print a table with lots of rows?

247,156

Solution 1

You can use the following:

<style type="text/css">
   table { page-break-inside:auto }
   tr    { page-break-inside:avoid; page-break-after:auto }
</style>

Refer the W3C's CSS Print Profile specification for details.

And also refer the Salesforce developer forums.

Solution 2

Wherever you want to apply a break, either a table or tr, you needs to give a class for ex. page-break with CSS as mentioned below:

/* class works for table row */
table tr.page-break{
  page-break-after:always
} 

<tr class="page-break">

/* class works for table */
table.page-break{
  page-break-after:always
}

<table class="page-break">

and it will work as you required

Alternatively, you can also have div structure for same:

CSS:

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

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

Div:

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

Solution 3

I have looked around for a fix for this. I have a jquery mobile site that has a final print page and it combines dozens of pages. I tried all the fixes above but the only thing I could get to work is this:

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

Solution 4

Unfortunately the examples above didn't work for me in Chrome.

I came up with the below solution where you can specify the max height in PXs of each page. This will then splits the table into separate tables when the rows equal that height.

$(document).ready(function(){

    var MaxHeight = 200;
    var RunningHeight = 0;
    var PageNo = 1;

    $('table.splitForPrint>tbody>tr').each(function () {

        if (RunningHeight + $(this).height() > MaxHeight) {
            RunningHeight = 0;
            PageNo += 1;
        }

        RunningHeight += $(this).height();

        $(this).attr("data-page-no", PageNo);

    });

    for(i = 1; i <= PageNo; i++){

        $('table.splitForPrint').parent().append("<div class='tablePage'><hr /><table id='Table" + i + "'><tbody></tbody></table><hr /></div>");

        var rows = $('table tr[data-page-no="' + i + '"]');

        $('#Table' + i).find("tbody").append(rows);
    }
    $('table.splitForPrint').remove();

});

You will also need the below in your stylesheet

    div.tablePage {
        page-break-inside:avoid; page-break-after:always;            
    }

Solution 5

this is working for me:

<td>
  <div class="avoid">
    Cell content.
  </div>
</td>
...
<style type="text/css">
  .avoid {
    page-break-inside: avoid !important;
    margin: 4px 0 4px 0;  /* to keep the page break from cutting too close to the text in the div */
  }
</style>

From this thread: avoid page break inside row of table

Share:
247,156
Mohammad Saberi
Author by

Mohammad Saberi

Updated on September 29, 2020

Comments

  • Mohammad Saberi
    Mohammad Saberi over 3 years

    I have a dynamic table in my web page that sometimes contains lots of rows. I know there are page-break-before and page-break-after CSS properties. Where do I put them in my code in order to force page breaking if needed?

  • David
    David over 11 years
    This successfully uses 4 pages for what is effectively a 2 page table and seems to have the same effect as no css in Safari (6) and Chrome (24).
  • jaredsmith
    jaredsmith over 10 years
    If you are trying this with a framework like bootstrap be aware that using something like .spanX (which styles with float) can disrupt page-break-* attributes, lost a lot of time myself to this little nuance.
  • aapierce
    aapierce over 10 years
    This only works if you know ahead of time exactly how many rows will fit on a page (since you have to declare the page breaks directly in your markup). This might be difficult (or impossible) if there's a possibility that the data in a table row might wrap to a second line. And what about multiple page sizes (A4 vs US Letter vs US Legal)?
  • JonEasy
    JonEasy almost 9 years
    this works with UIWebView so I assume this will also work with Safari now
  • Hemant Metalia
    Hemant Metalia over 8 years
    David thanks for sharing solution to work with safari or charome as well. JonEasy you can use solution of David in case this solution doesnt work with Safari. @Maxence glad it helped. Sorry I was not active for few months so was not able to respond early.
  • Daniel Hollands
    Daniel Hollands over 8 years
    Sorry, but I'm unable to see what solution David provided (I'm also having the issue of this not working)
  • joetsuihk
    joetsuihk almost 7 years
    in addition to @jaredsmith about .spanX, <section> as well
  • damianostre
    damianostre over 6 years
    This solution does not break multi-line cells cleanly between their cell borders, but breaks them up in the middle. I've found a solution, though, which works in all current browsers (Safari, FF and Chrome): stackoverflow.com/a/47498775/43615
  • damianostre
    damianostre over 6 years
    This solution did not work for me when using multi-line cells and multiple columns. I found a solution to that, though: stackoverflow.com/a/47498775/43615 - It also deals with the issue @aapierce mentions.
  • Avatar
    Avatar over 6 years
    Couldn't get the breaking tr to work either in Chrome v64.
  • Chris
    Chris over 5 years
    Doesn't do anything when the table has one large row.
  • JJJ
    JJJ almost 5 years
    Manually dividing the table to pages isn't a very good solution unless the content is something that should naturally be printed on separate pages (e.g. financial information printed one page per year.) You don't know beforehand how much content will fit on one printed page.
  • Avinash Dalvi
    Avinash Dalvi over 4 years
    this works somewhat extent. Means its stop cutting data in row at bottom. Row its still cutting. but at least better than actual issue
  • Saif
    Saif about 4 years
    this doesn't work for me on chrome, the only solution which worked was to wrap content on each td inside a div since page-break-inside appears to be only working on display: block elements,
  • Hemant Metalia
    Hemant Metalia about 4 years
    @Saif this answer was provided few years back, it may possible that it does not comply with newer versions of browser. but you may get some hint through it.
  • andrejs82
    andrejs82 almost 4 years
    That not working for me :( I am using CHOME 83.0.4103.116
  • andrejs82
    andrejs82 almost 4 years
    This is work in table if that html put between two TR tags ( CHROME ). This is NOT work in table if that html put between two TR tags ( FIREFOX ). In FileFox work CSS Rule-> table tr:nth-child(10) {page-break-after: always;page-break-inside: avoid;}