Printing tables with forced page breaks

10,832

Begin with using correct HTML with closing tags

if ($condition) {
   echo ( "</tr>" );
   echo ( "<tr style='page-break-after:always;'>" );
   echo ( "</tr>" );
} else {
   echo ( "</tr>" );
}

If that doesn't help try

if ($condition) {
   echo ( "</tr>" );
   echo ( "<tr style='page-break-after:always;'>" );
   echo ( "</tr> " );
   echo ( ' <tr class="class_only_visible_when_printed"> ' );
   echo ( "  <th>Header 1</th> " );
   echo ( "  <th>Header 2</th> " );
   echo ( "  <th>Header 3</th> " );        
   echo ( " </tr> " );
} else {
   echo ( "</tr>" );
} 
Share:
10,832
imhere
Author by

imhere

Updated on June 04, 2022

Comments

  • imhere
    imhere almost 2 years

    I have a long table with hundreds of rows. To print the table headers on each page, I am using THEAD {display: table-header-group} to style the table. And it works as expected in Firefox and IE9. But when I try to force the page breaks as shown in below code, it doesn't work in IE9. With IE9, the table headers get printed only the pages where the table breaks naturally.

    Here is the code snippet -

    CSS: 
    @media print {
        thead { display: table-header-group; }
    }
    
    HTML/PHP:
        <table>
          <thead>
            <tr>
               <th>Header 1</th>
                   <th>Header 2</th>
                   <th>Header 3</th>        
               </tr>
          </thead>
          <tbody>
               <?php 
               foreach ($items as $item) {
                   echo  " <tr> " ;
                      echo  "<td>text</td>" ;
                      echo  "<td>text</td>" ;
                      echo  "<td>text</td>" ;
    
                      if ($condition) {
                          echo  "</tr>" ;
    
                          echo  "<tr style='page-break-after:always;'>" ;
                          echo  "</tr>" ;
                      }
               }
               ?>
           </tbody>
        </table>
    

    Will greatly appreciate any help on this.

    Thanks.