Search code examples
phpcodeigniterforeachdompdfpage-break

dompdf page break with codeigniter and foreach loop


I am using codeigniter and dompdf to generate a pdf document.

the document is generated with a foreach loop. each loop creates a few table rows. I want to have a pagebreak between the rows of each loop.

my relevent codeigniter view syntax is:

<table width=100%>
<?php foreach($summary as $summary){  ?>
<tr class="firstrow">
<td colspan="10">
<?php echo  "<b>Customer</b>: <font color='blue'>".$summary->caccountname; ?>

<?php echo  "</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <b>Order Number</b>: <font color='blue'>".$summary->OrderNum; ?>

<?php echo  "</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <b>Sales rep</b>: <font color='blue'>".$summary->RepName; ?>

<?php echo  "</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <b>Due Date</b>: <font color='blue'>".substr($summary->DueDate,0,10); ?>
</td>
</tr>
<tr>
<td colspan="10"><hr>
</td>
</tr>
<tr>
<td colspan="10">

  <table class="sortable" width="90%">
  <tr><td ><b>Product</td><td ><b>Ordered</td><td ><b>Processed</td><td ><b>Outstanding</td><td ><b>Dispatched</td><td ><b>Available (incl FJ)</td><td ><b>Available FJ</td></tr>
  <?php foreach($$linedetails as $linedetails){  ?>
  <tr>
  <td><?php echo $linedetails->Product;  ?></td>
  <td><?php echo $linedetails->cubicvolume;  ?></td>
  <td><?php echo $linedetails->Processed;  ?></td>
  <td><?php echo $linedetails->Outstanding;  ?></td>
  <td><?php echo $linedetails->Dispatched;  ?></td>
  <td><?php echo $linedetails->TotalVolume;  ?></td>
  <td><?php echo $linedetails->FJVolume;  ?></td>
  </tr>
  <?php } ?>
  </table>
</td>
</tr>


<?php } ?>
</table>

I have given the first tr a class of firstrow, I would like a page break to appear before this line every time.

As such I have applied the following style sheet in my page header:

<style>
 @media print
 {
 .firstrow {page-break-before:always}
 }
</style>

This doesnt work.

How can I acheive my desired result of placeing a page break before each loop firstrow TR?

Thanks as always


Solution

  • Try this, I am doing reports exactly same way

    <?php 
    $nbsp5 =  "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
    
    foreach($summary as $summary) {  ?>
        <table width="100%" style="page-break-after:always;">
            <tr>
                <td colspan="10">
                    <?php echo  "<b>Customer</b>: <font color='blue'>".$summary->caccountname; ?>
                    <?php echo  "</font>".$nbsp5 ." <b>Order Number</b>: <font color='blue'>".$summary->OrderNum; ?>
                    <?php echo  "</font>".$nbsp5 ." <b>Sales rep</b>: <font color='blue'>".$summary->RepName; ?>
                    <?php echo  "</font>".$nbsp5 ." <b>Due Date</b>: <font color='blue'>".substr($summary->DueDate,0,10); ?>
                </td>
            </tr>
            <tr>
                <td colspan="10"><hr></td>
            </tr>
            <tr>
                <td colspan="10">
                  <table class="sortable" width="90%">
                    <tr>
                        <td><b>Product</b></td>
                        <td><b>Ordered</b></td>
                        <td><b>Processed</b></td>
                        <td><b>Outstanding</b></td>
                        <td><b>Dispatched</b></td>
                        <td><b>Available (incl FJ)</b></td>
                        <td><b>Available FJ</b></td>
                    </tr>
                  <?php foreach($$linedetails as $linedetails){  ?>
                  <tr>
                      <td><?php echo $linedetails->Product;  ?></td>
                      <td><?php echo $linedetails->cubicvolume;  ?></td>
                      <td><?php echo $linedetails->Processed;  ?></td>
                      <td><?php echo $linedetails->Outstanding;  ?></td>
                      <td><?php echo $linedetails->Dispatched;  ?></td>
                      <td><?php echo $linedetails->TotalVolume;  ?></td>
                      <td><?php echo $linedetails->FJVolume;  ?></td>
                  </tr>
                  <?php } ?>
                  </table>
                </td>
            </tr>
    
        </table>
    <?php } ?>
    

    (first <table> tag moved inside loop)

    Please try this answer for both header and footer.