dompdf inserts blank page at end of document

how to avoid extra blank page at end while printing
mpdf remove blank pages
dompdf documentation
page-break-after avoid blank page
css avoid blank page
dompdf page-break
remove blank pages from pdf automatically
dompdf table multiple pages

I'm generating a pdf document using dompdf 0.6.0, and have a strange issue where a blank page is being created at the end. My (simplified) html:

<!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;
}
</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="page" style="background-image: url(page3.jpg); color: white;">
...
</div>
</body>
</html>

The first three pages look amazing, but there is a blank page at the end. I've read dompdf is picky about nesting and compliance and such, but the html is super clean and checks out.

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.

Blank page inserted at end of document. · Issue #211 · dompdf , I'm using the latest version of dompdf and it generated blank pages at the end of the document. I think it happens because of nested elements  For what it's worth, I have been trying to produce a document template/container on screen that looks similar to its output through dompdf 0.6.2.

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

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

Generating blank pages at the end of the · Issue #1511 · dompdf , DOMPDF6 (pulled from 02/26/2014) DOMPDF generates blank page after Note that the height of .document plus the top and bottom margins  Generate Blank PDF file is empty #363. Closed PhenX it's just blank. So dompdf is running but somewhere along the line the text is not parsed correctly.

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!

Full-page normal-flow block element causes blank page insertion , I'm generating a pdf document using dompdf 0.6.0, and have a strange issue where a blank page is being created at the end. My (simplified) html: <!DOCTYPE​  Whenever I use page-break-inside:avoid on an element that is larger than one page, the entire element is squished into the top left corner, and I get all sorts of other issues. Right now I'm forced to comment out the page-break-inside:avoid and just have the element continue on the next page, since there's no way to solve it.

If you are using an array to store and output your HTML code, and still getting the error, you can remove the last page including page breaks in all pages except the last one, using count() function.

$html='';
$i = 0; //counter
$len = count($output_array);// lenght of the array
foreach($output_array as $output)
{   //store the html in a variable to use it in dompdf

    $html.= $output;

    if ($i < $len - 1) 
    { //if the counter is less than the lenght of the array, add a page break.
      $i++; 
      $html.=' <div class="page_break"></div>';
    }
}

dompdf inserts blank page at end of document, table { border: none; page-break-after: always; } Not surprisingly, this results in a this div after my last table) but the css doc site says that I can't use an empty div​. tweaking like remove something else just to get it to end a wee bit sooner? Those values are correct as of when that text is evaluated. What you want to do rather than interpreting the text on the current page is to insert the value after the document is rendered.

Blank last page on page-break-after - HTML & CSS, blank page at the end every time! To fix this just change it to page-break-after:​auto. It will break correctly and not create an extra blank page. Whitespace inside HTML tag breaks rendering #1494. use Dompdf\FrameDecorator\Page as pages before and another empty page after "my document"--blank page

Stop blank page being created (because of page break setting) at , In my case a blank page meant a rendering error. Check your server's error_log file and you might see error messages from DOMPDF basically  Dompdf is a PHP library that provides a simple way to convert HTML to PDF document. Using the Dompdf library you can easily generate PDF from the HTML page in PHP. The example code will help you to implement PDF generation functionality in the web application and make it simple to convert HTML to PDF in PHP with Dompdf.

Blank page with no errer reporting - HELP!, The following example generates a PDF of 2 pages with margins of the bottom: -60px; left: 0px; right: 0px; height: 50px; /** Extra personal  You can choose to delete Current Page (the default setting), or Pages within a range. Note: Deleting a page is a permanent action upon saving the document. If you change your mind after making a deletion, close the document without saving the changes, re-open it, and start over. Click OK when you have finished making your selections to delete

Comments
  • I think the problem is not caused by the closing </body> and </html> tags, but actually by the newline that comes after the closing </div> tag. Putting both the </body> and </html> on the same line as the last </div> will also work.
  • Interesting. Good catch @Joe. I'll try to verify that and update my answer.
  • @Joe It works like a charm, This is the perfect answer. Thanks
  • Thanks! I would have never in a million years thought to look at the closing html and body tags.
  • This, and maybe even a trim() on the html string you're about to transform into PDF. An empty space after </html> could make your head ache :)
  • Yep, that was it for me as well