124

I want to avoid page break inside row of table in html, when I convert html to PDF by wkhtmltopdf. I use page-break-inside:avoid with table- its works, but I have so many rows, then not work. If set display of tr as block or some thing else then it change the formatting of table and insert double border. Or it is possible to insert the table header on each page, where the table was splitted.

Ankit Mittal
  • 1,330
  • 2
  • 9
  • 11
  • 1
    Sorry, what is the problem when using `page-break-inside: avoid;`? – Christian Feb 15 '12 at 07:29
  • 2
    @ChristianVarga when I use page-break-inside:avoid with tr, it is not work – Ankit Mittal Feb 15 '12 at 07:52
  • Tried putting it on the table element instead? – Christian Feb 15 '12 at 09:26
  • 1
    The page breaking with tables is quite buggy. Have a look at this JavaScript workaround http://code.google.com/p/wkhtmltopdf/issues/detail?id=168#c4 – Jona Feb 15 '12 at 09:30
  • 2
    actually I have tried `page-break-inside:avoid` with all the table elements like tr td, but it not worked. – Ankit Mittal Feb 16 '12 at 07:52
  • @Jona My friend I have tried this all already, but nothing is perfectly work – Ankit Mittal Feb 16 '12 at 07:54
  • 3
    Yay Google for WebGL, blinding fast javascript, portable native client, but we still can't print data tables. Who would need to do that??? Only just about every business in the world. Incidentally, I tried to print a spreadsheet in google docs just now, and it consistently crashes my chrome. I think google docs prints via pdf. :/ – Sam Watkins May 20 '14 at 08:05

13 Answers13

96

You might try this with CSS:

<table class="print-friendly">
 <!-- The rest of your table here -->
</table>

<style>
    table.print-friendly tr td, table.print-friendly tr th {
        page-break-inside: avoid;
    }
</style>

Most CSS rules don't apply to <tr> tags directly, because of exactly what you pointed out above - they have a unique display style, which doesn't allow for these CSS rules. However, the <td> and <th> tags within them usually do allow this kind of specification - and you can easily apply such rules to ALL child-<tr>'s and <td>'s using CSS as shown above.

Troy Alford
  • 26,660
  • 10
  • 64
  • 82
  • 24
    Unfortunately this doesn't (yet) work with webkit based browsers. – Attila Fulop Mar 12 '13 at 19:17
  • 2
    Yes - there are some oddities. See @Peter's answer in this question: http://stackoverflow.com/questions/7706504/page-break-inside-doesnt-work-in-chrome for some more info. – Troy Alford Mar 13 '13 at 02:08
  • 3
    It only works if you take the entire table, not just tr/td: http://stackoverflow.com/a/13525758/729324 – marcovtwout Dec 30 '13 at 13:36
  • 1
    I've run into issues with this in IE8, possibly others, where it causes the entire table to try to fit on one page and cuts off any overflow. It also seemed to ignore the "scale to fit" option for these tables. – Tom Pietrosanti Jan 23 '14 at 17:00
  • 1
    Yes. :( Unfortunately IE's implementations in earlier browser versions have a tendency to behave in very non-standards-compliant ways. – Troy Alford Jan 24 '14 at 18:47
  • 6
    @AttilaFulop Still? Your post is 3 years old and I still can't get it to work. Thanks – relipse May 13 '16 at 21:41
  • 1
    @relipse I too couldn't get it to work, But for me did the work – Ammy T Jan 25 '17 at 05:54
  • 1
    Looks like [`page-break-inside`](https://developer.mozilla.org/en-US/docs/Web/CSS/page-break-inside) has been deprecated and been replaced with [`break-inside`](https://developer.mozilla.org/en-US/docs/Web/CSS/break-inside) – KyleMit Sep 01 '19 at 19:27
  • Firefox still breaks the table row between pages in the print preview but in the actual paper print it works fine. – GDavoli Nov 02 '20 at 01:52
  • Still doesn't work. It's broken when printing to PDF as well. – mbomb007 Mar 14 '22 at 20:38
40

The best way I have found to deal with this problem in webkit browsers is to put a div inside each td element and apply the page-break-inside: avoid style to the div, like this:

...
<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>

Even though Chrome supposedly does not recognize the 'page-break-inside: avoid;' property, this seems to keep the row content from being split in half by a page break when using wktohtml to generate PDFs. The tr element may hang over the page break a bit, but the div and anything inside it will not.

Aaron Hill
  • 636
  • 6
  • 14
  • The `margin: 4px 0 4px 0;` did the trick for me, i was losing a record occasionally. Thanks – Wartodust Sep 24 '15 at 07:26
  • A problem with this approach is that sometimes only a few cells of a row are broken to the next page, although each cell on its own will not get broken. – WorldSEnder Mar 22 '17 at 12:27
  • @WorldSEnder Yes. Did you find a work around for this ? – ranjansaga Jun 30 '17 at 11:49
  • 8
    Doesn't work. Nothing works. I tried every "solution" on the internet and there is no solution. We just have to accept crap software that can't even render a table. – Niklas Rosencrantz Oct 20 '17 at 13:15
25

I used the 4px trick by @AaronHill above (link) and it worked really well! I used though a simpler css rule without needing to add a class to each <td> in the table.

@media print {
    table tbody tr td:before,
    table tbody tr td:after {
        content: "";
        height: 4px;
        display: block;
    }
}
mbomb007
  • 3,788
  • 3
  • 39
  • 68
phidias
  • 518
  • 4
  • 6
17

I've found a new way to solve this problem, at least for me (Chrome Version 63.0.3239.84 (Official Build) (64-bit) on MacOS Sierra)

Add a CSS rule to the parent table:

table{
    border-collapse:collapse;
}

and for the td:

tr td{
    page-break-inside: avoid;
    white-space: nowrap;
}

I actually found this solution on Stack Overflow, but it didn't show up in initial Google searches: CSS to stop page break inside of table row

Kudos to @Ibrennan208 for solving the problem!

Jason Silver
  • 527
  • 7
  • 23
11

I have found that page-break-inside: avoid will not work if the any of the table's parent elements are display: inline-block or flex. Make sure all parent elements are display: block.

Also consider overriding table, tr, td's display styles with CSS grid for the print layout if you keep having issues with the table.

Stephen Saucier
  • 1,925
  • 17
  • 20
  • I fixed my multiple tables being broken up in weird places with overlapping text in Chrome, with help from this question: added `display:block !important;` to `body` and `div` when printing, as well as `page-break-inside: avoid;` to `table`. – Martin Hansen Dec 21 '20 at 11:16
  • I was about to give up but this answer give me the right threat to pull on. I am using tailwind and had `flex` classes applied to some containing elements. I added `print:block` to those and my table suddenly started respecting the page-break-inside directives. Thank you! – markquezada Jan 04 '23 at 03:39
9

Using CSS page-break-inside won't work (this is a webkit browser issue).

There is a wkhtmltopdf JavaScript table splitting hack which breaks a long table into smaller tables automatically depending on the page size you specify (rather than page breaking after a static value of x rows): https://gist.github.com/3683510

Richard Pursehouse
  • 1,109
  • 13
  • 21
9

The only way I found to work was to place each TR element inside it's own TBODY element, and apply the page break rules to the TBODY element via css

Troy Morehouse
  • 5,320
  • 1
  • 27
  • 38
  • 1
    This works better than the rest for me on Chromium Version 40.0.2214.111 (64-bit) on Arch Linux. It's ugly and feels hacky - but apparently multiple `tbody` elements are valid inside a `table` http://stackoverflow.com/questions/3076708/can-we-have-multiple-tbody-in-same-table – ElDog Feb 24 '15 at 09:57
  • This does not work for me (Chrome 56.0.2924.87 (64-bit), Mac OS 10.12.2). – Nhat Dinh Feb 20 '17 at 08:06
  • Thanks! This made me find a solution that worked for me - https://stackoverflow.com/a/71952985/11747650 – Karolis Vaitkevicius Apr 21 '22 at 10:34
8

I wrote the following JavaScript based on Aaron Hill's answer:

//Add a div to each table cell so these don't break across pages when printed
//See http://stackoverflow.com/a/17982110/201648
$(document).ready(function () {
    var ctlTd = $('.dontSplit td');
    if (ctlTd.length > 0)
    {
        //console.log('Found ctlTd');
        ctlTd.wrapInner('<div class="avoidBreak" />');
    }
});

Where dontSplit is the class of the table where you don't want the td's to split across pages. Use this with the following CSS (again, attributed to Aaron Hill):

 .avoidBreak {
    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 */
  }

This appears to be working nicely in the latest version of Chrome.

Aaron Newton
  • 2,124
  • 1
  • 28
  • 31
  • 1
    I am rendering large tables into PDF format using wkhtmltopdf, and this is the only solution that yielded acceptable results. It could be better (Webkit doesn't repeat cell borders after a page break), but at least the content is there. Thanks! – Jonathon Hill Jan 01 '14 at 06:33
  • This doesn't work when printing using Ctrl+P – mbomb007 Mar 15 '22 at 13:26
8

The 2020 solution

The only thing I could consistently get to work on all browsers is to put each row inside its own table element. This also works with node HTML-PDF. Then just set everything to page-break-inside: avoid.

table,
tr,
td,
div {
    page-break-inside: avoid;
}

The only disadvantage to this is that you must manually set the width of the columns, otherwise it looks rather strange. The following worked well for me with two columns.

td:first-child { width: 30% }
td:last-child { width: 70% }


Example

<table>
  <tr>
    <td>Forename</td>
    <td>John</td>
  </tr>
</table>
<table>
  <tr>
    <td>Surname</td>
    <td>Piper</td>
  </tr>
</table>
<table>
  <tr>
    <td>Website</td>
    <td>desiringgod.org</td>
  </tr>
</table>
gilbert-v
  • 1,263
  • 1
  • 11
  • 23
7

Try with

white-space: nowrap; 

style to td to avoid it breaking on new lines.

vinayakshukre
  • 185
  • 1
  • 10
0

This is an old question but I got into this same error recently. The problem in my case was related to Bootstrap's table-responsive class, which was accidentally being used on <table> element.

So, if you have the same issue, try removing table-responsive from <table> class when calling wkhtmltopdf.

Laerte
  • 7,013
  • 3
  • 32
  • 50
0

To avoid wrong page breaks with table rows, need to use the below CSS while generating PDF files from HTML.
table tr { page-break-inside: avoid! important;margin: 4px 0 4px 0;} enter image description here

Sher Singh
  • 497
  • 5
  • 8
0

This is what worked for me:

@media all {
  table.report { page-break-after:auto }
  table.report tr    { page-break-inside:avoid; page-break-after:auto }
  table.report td    { page-break-inside:avoid; page-break-after:auto }
  table.report thead { display:table-header-group }
  table.report tfoot { display:table-footer-group }
}
Codeparl
  • 300
  • 1
  • 8