48

What is the best way to do page breaks in dompdf?

I have had a look here at the page-break-before css attribute, but it didn't work when I did:

table {page-break-before:auto;}

The page still breaks in the middle of my table.

Is it possible to setup my html/css so that the page will break before the element if the element is going to exceed the page height?

Ideally I would like to divide my html up in to div sections so that each section will start on a new page if it is going to exceed the height of the current page.

5 Answers 5

104

Using page-break-inside: auto; basically says to dompdf "do what you would normally do when breaking pages."

To force a page break before / after your table you would use page-break-before: always; / page-break-after: always;.

To ask dompdf to avoid breaking inside an element you would use page-break-inside: avoid;.

5
  • page-break-inside:avoid; made disappear images in table and has an issue especiall when using colspan=2 in my case Nov 18, 2014 at 9:17
  • 10
    Just a heads-up: I tried using the page-break-inside: avoid on <tbody/> and <table/> tags, as well as using page-break-after: avoid on a <tr/> – both gave me some extra empty pages. My solution was to wrap the tables in a <div/> with page-break-inside: avoid applied. Aug 10, 2015 at 17:35
  • note this was not working with rowspan in tds, I finally switched to mpdf because it was a very simple (but long) table
    – steinkel
    Dec 6, 2016 at 18:32
  • 2
    page-break-inside: auto; doesn't work with elements larger than the pages UNFORTUNATELY...see: github.com/dompdf/dompdf/issues/653 and github.com/dompdf/dompdf/issues/809
    – TOPKAT
    Nov 20, 2017 at 10:24
  • This is still a valid fix. I was unable to get my large table converted to PDF without cutting off content past the first page. The css rule page-break-after: always; Saved my but. I just add that rule inline every time the content gets close to the page height. Big thanks for this little trick!
    – kenef
    Oct 22, 2019 at 16:41
11

You might make quick tests with this online debugger - i finally found my pagebreak and margin issue after days of testing.

Excursus: Did anyone install a debug environment on the development/production environment and can point me to any documentation or tutorial?

2
  • Makes sense checking the possible issues on this debugger. For example, a small change in CSS can affect big. Especially, the dompdflog tab is very handy.
    – Fr0zenFyr
    Jul 23, 2015 at 11:46
  • Thanks for that debugger link, it's very useful.
    – JoshGeake
    May 20, 2020 at 12:16
5

Here's a trick: place the <table> you do NOT want to print across multiple pages in another <table>.

1
  • 2
    or place it into <div>
    – Paktas
    Feb 4, 2014 at 17:46
3

In my case it was happened since I have used a table inside another table. Like,

<table>
   <tr>
      <td>
         <table></table>
      </td>
   </tr>
 </table>

So I did was getting the table out. Solved my issue.

0

//Firstly assign a variable before starting for loop and set post-increment operator inside for loop. secondly, use with condition of data display item in a single page.

  <?php $n=1 ?>
          @foreach ($purchases as $key=> $purchase)
               <tr >
                   <td> {{ $key + 1 }}  </td>
                   <td> {{ $purchase->supplier->company_name ? 
                   $purchase->supplier->company_name : "" }}  </td>
                   <td> {{ "S-".$purchase->id  }}   </td>
                   <td> {{  salte_items($purchase->purchaseItems) }}        </td>
                   <td> {{ $purchase->created_at->format('d-m-Y') }}  </td>
                   <td> {{ intval($purchase->total) }}    </td>
                    </tr>
                  
                    @if ( $n % 25 == 0 )
                        <div style="page-break-before:always;"> </div>
                    @endif
                    <?php $n++ ?>

           @endforeach
2
  • 1
    Please add comments and explain your code Dec 22, 2020 at 12:37
  • 1
    you can add your comment to your code by editing your answer Dec 23, 2020 at 11:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.