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.

startupsmith
  • 5,554
  • 10
  • 50
  • 71

5 Answers5

103

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;.

lmeurs
  • 16,111
  • 4
  • 27
  • 30
BrianS
  • 13,284
  • 15
  • 62
  • 125
  • `page-break-inside:avoid;` made disappear images in table and has an issue especiall when using `colspan=2` in my case – andreas-supersmart Nov 18 '14 at 09:17
  • 10
    Just a heads-up: I tried using the `page-break-inside: avoid` on `` and `
    ` tags, as well as using `page-break-after: avoid` on a `` – both gave me some extra empty pages. My solution was to wrap the tables in a `
    ` with `page-break-inside: avoid` applied.
    – ᴍᴇʜᴏᴠ Aug 10 '15 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 06 '16 at 18:32
  • 2
    `page-break-inside: auto;` doesn't work with elements larger than the pages UNFORTUNATELY...see: https://github.com/dompdf/dompdf/issues/653 and https://github.com/dompdf/dompdf/issues/809 – TOPKAT Nov 20 '17 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 '19 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?

andreas-supersmart
  • 4,114
  • 2
  • 18
  • 13
  • 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 '15 at 11:46
  • Thanks for that debugger link, it's *very* useful. – JoshGeake May 20 '20 at 12:16
5

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

2

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.

Viraj Amarasinghe
  • 911
  • 10
  • 20
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