Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing HTML tables without splitting a row across two pages

I need to print some data in an HTML table from Internet Explorer (various versions) and Safari on OS X. Ignoring Safari for the moment.

When printing a table that has more rows than can fit on a page, the last row on the page is often split with part of the row on the first page and the rest of the row on the next page.

This looks ugly. Is there a way to avoid it?

I've been searching around and so far I've found page-break-before: always, but I'm not sure if that's what I'm looking for, since I won't know up front how many rows will fit on the page. e.g. the rows might be taller or shorter depending on the content and the user might be printing in portrait or landscape mode. I'm assuming A4 at least, but if the user was using US letter or some other paper size it would introduce even more uncertainty. Also, what about printer margins?

So is there a way around this? Or do I just have to guess how many rows will fit and force a page break after that? Will that even work?

like image 837
Wodin Avatar asked Jul 05 '11 17:07

Wodin


3 Answers

You probably want to play around with page-break-before/after: auto. page-break-inside would be ideal but its not supported by most browsers.

Take a look here http://www.westciv.com/style_master/academy/css_tutorial/advanced/printing.html for some good info.

like image 184
Mrchief Avatar answered Oct 23 '22 13:10

Mrchief


How about page-break-inside:avoid?

There seem to be a lot of talk about this, however I did also find another SO article mentioning page-break-before:auto and applying to each table cell.

like image 22
Brad Christie Avatar answered Oct 23 '22 15:10

Brad Christie


A few simple solutions do exist for preventing page breaks within rows, but they have some pretty big limitations that tend to make them useless for anything but intranet stuff.

A more versatile solution that's only slightly more complicated can be found here. Its main drawback is that it requires fixed column widths.

My favorite solution is quite a bit more complicated, but it will probably cause the fewest headaches in the end. It uses JavaScript to turn every row into a separate, unbreakable block (but not in a way that affects the table's appearance). I've posted a few answers that use this technique:

  • Answer 1 (best)
  • Answer 2
like image 38
DoctorDestructo Avatar answered Oct 23 '22 15:10

DoctorDestructo