dompdf inserts blank page at end of document

In my case it occurred because the content was bigger than the paperSet(). If the content pass at least 1px, dompdf automatically creates another sheet. So you always need to set sizes according the paperSet() proportion you set.

For example: Paper A4 is 210mmx297mm. To make it easier, I recommend to use 'mm' instead of 'px'. Then you can create your divs and add their heights summing them all and making sure it is within 297mm.

To know the papers sizes you can see in this site:

https://papersizes.io/


Turns out the end </body> and </html> tags were causing the extra page. I removed them, and results are as expected.

I'd imagine its a problem with dompdf, but I spent quite awhile trying to solve the issue and figured this might be of help to others.

Update:

As Joe mentions in the comments, moving the </body> and </html> tags to the same line as your closing </div> works, and remains valid html.


For me the fix was to remove any whitespace between tags.

 $html = preg_replace('/>\s+</', "><", $html);

This happens because of the css you have written page-break-after: always; for page class. The above style will do a page break after your last page also and dompdf creates a blank page at the end.

Another solution is to write a separate css style for your last page excluding page-break-after: always;

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>PDF</title>
<style type="text/css">
@page{ margin: 0;}

.page{
    width: 612px; 
    height: 792px; 
    overflow: hidden; 
    font-family: Arial, Helvetica; 
    position: relative; 
    color: #545554;
    page-break-after: always;
}
.last-page{
   width: 612px; 
   height: 792px; 
   overflow: hidden; 
   font-family: Arial, Helvetica; 
   position: relative; 
   color: #545554;
 }

</style>
</head>
<body>
<div class="page" style="background-image: url(page1.jpg);">
...
</div>

<div class="page" style="background-image: url(page2.jpg);"></div>

<div class="last-page" style="background-image: url(page3.jpg); color: white;">
...
</div>
</body>
</html>

Happy coding!

Tags:

Php

Dompdf