Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

boby's avatar
Level 2

barryvdh/laravel-dompdf table page break problem

Hi,

so I am using barryvdh/laravel-dompdf to generate invoice table, but the problem is on page break.

css:

.page-break {
page-break-before: always;
}

in my blade file:

<tbody>
	@foreach($invoice->articles as $article)
		<tr>
			<td>
	  			{{ $loop->iteration }} 
	  		</td>
	  		<td>
	  			{{ $article->name }} 
	  		</td>
	  		<td>
	  			{{ $article->quantity }} 
	  		</td>
			...
	  	</tr>
	  	@if($loop->iteration == 13)
	        	<div class="page-break"></div>
	        @endif
	  @endforeach
</tbody>

I am braking table after 13 items, and everything is fine, but the row on item 14 is screwed because of this div I assume:

https://www.dropbox.com/s/aupu3tu4wwx2yel/Screenshot_20200329_115614.png?dl=0

0 likes
10 replies
Snapey's avatar
Snapey
Best Answer
Level 122

the docs say that tables cannot span a page break. You need to close the table before the break, then open a new one.

boby's avatar
Level 2

Perfect! Works like a charm :)

@if($loop->iteration == 13)
	</tbody>
</table> <!-- Table closed -->
<div class="page-break"></div> <!-- Page break -->
<table class="table invoice-articles-table">
	<thead>
		<tr>
			<th>#</th>
			...
	</thead>
	<tbody>
@endif

Is there a better way to break than this "manual" on line 13? I am counting here manually, but is there some better way, which will use as much space as possible, which won't break if I change some css (e.g. row height)?

Thank you!

Snapey's avatar

id like to know also. I don't believe so. This means you end up playing safe and leaving a wide margin incase some table lines wrap.

boby's avatar
Level 2

@snapey in case you still need it

Before content, in my case articles table, put this to blade file:

<div style="page-break-after:auto;">

in css file for your blade:

.page-break {
    page-break-before: always;
}

.invoice-articles-table {
    padding-bottom: 50px; //height of your footer
}

Boom! You have auto page brake no matter how many lines in the table :)

JoaoHamerski's avatar

@boby where should i put the "page-break-after:auto" ? I have the same problem, i have some tr's and need the page break automatically when get the end of page.

JoaoHamerski's avatar

should i wrap all the table in this div "page-break-after:auto"?

boby's avatar
Level 2

Hi @joaohamerski

So I use it like this:

<body>

	<header>
		Logo and other header stuff
	</header

	<footer>
		Page number and footer stuff
	</footer

	<div style="page-break-after:auto;">

	<div class="container my-content">
		<div class="row">
			<table>
				My content here
			</table>
		</div>
	</div>
</body

In your css file add padding for the table: padding-bottom: 50px;

so it won't overlap with the footer

Hope it helps

JoaoHamerski's avatar

@boby it didn't work to me, maybe it doesnt work in table that have rowspans? What is this "row"? I dont have a "row" class wrapping my table, can you show me the style of the row please? And the container?

boby's avatar
Level 2
<div class="container">
	<div class="row"> 

are standard bootstrap grid classes. I don't have any styles on them, I only have bootstrap css included in my blade file:

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">

Try to remove rowspans, and make simple table to test this, then apply it, to your solution.

It took me a while before I found working solution. You need to try some combinations as well, try bootstrap, try without, create simple table with "dummy" data, etc...

JoaoHamerski's avatar

That is how i solved my problem:

i created a table for each record, as i had a rowspan the examples showed didn't work to me,

so

	@foreach($items as $item)
		<table>
			....
		</table>
	@endforeach

So in my css i put:

table {
	page-break-inside: avoid !important;
}

That worked perfectly, didn't break the images i had in the table.

Please or to participate in this conversation.