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

前端 未结 12 1847
有刺的猬
有刺的猬 2020-11-22 16:44

I have a project which requires printing an HTML table with many rows.

My problem is the way the table is printed over multiple page. It will sometimes cut a row in

相关标签:
12条回答
  • 2020-11-22 17:31

    I've tried all suggestions given above and found simple and working cross browser solution for this issue. There is no styles or page break needed for this solution. For the solution, the format of the table should be like:

    <table>
        <thead>  <!-- there should be <thead> tag-->
            <td>Heading</td> <!--//inside <thead> should be <td> it should not be <th>-->
        </thead>
        <tbody><!---<tbody>also must-->
            <tr>
                <td>data</td>
            </tr>
            <!--100 more rows-->
        </tbody>
    </table>
    

    Above format tested and working in cross browsers

    0 讨论(0)
  • 2020-11-22 17:38

    I recently solved this problem with a good solution.

    CSS:

    .avoidBreak { 
        border: 2px solid;
        page-break-inside:avoid;
    }
    

    JS:

    function Print(){
        $(".tableToPrint td, .tableToPrint th").each(function(){ $(this).css("width",  $(this).width() + "px")  });
        $(".tableToPrint tr").wrap("<div class='avoidBreak'></div>");
        window.print();
    }
    

    Works like a charm!

    0 讨论(0)
  • 2020-11-22 17:39

    None of the answers here worked for me in Chrome. AAverin on GitHub has created some useful Javascript for this purpose and this worked for me:

    Just add the js to your code and add the class 'splitForPrint' to your table and it will neatly split the table into multiple pages and add the table header to each page.

    0 讨论(0)
  • 2020-11-22 17:42

    I ended up following @vicenteherrera's approach, with some tweaks (that are possibly bootstrap 3 specific).

    Basically; we can't break trs, or tds because they're not block-level elements. So we embed divs into each, and apply our page-break-* rules against the div. Secondly; we add some padding to the top of each of these divs, to compensate for any styling artifacts.

    <style>
        @media print {
            /* avoid cutting tr's in half */
            th div, td div {
                margin-top:-8px;
                padding-top:8px;
                page-break-inside:avoid;
            }
        }
    </style>
    <script>
        $(document).ready(function(){
            // Wrap each tr and td's content within a div
            // (todo: add logic so we only do this when printing)
            $("table tbody th, table tbody td").wrapInner("<div></div>");
        })
    </script>
    

    The margin and padding adjustments were necessary to offset some kind of jitter that was being introduced (by my guess - from bootstrap). I'm not sure that I'm presenting any new solution from the other answers to this question, but I figure maybe this will help someone.

    0 讨论(0)
  • 2020-11-22 17:43
    <!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>
    
    0 讨论(0)
  • 2020-11-22 17:43

    Use these CSS properties:

    page-break-after
    
    page-break-before 
    

    For instance:

    <html>
    <head>
    <style>
    @media print
    {
    table {page-break-after:always}
    }
    </style>
    </head>
    
    <body>
    ....
    </body>
    </html>
    

    via

    0 讨论(0)
提交回复
热议问题