How I can break my document correctly with DOMPDF?

让人想犯罪 __ 提交于 2019-12-12 05:27:30

问题


I want to generate a PDF with DOMPDF through dynamic content. I am creating a table like this:

<?php

$language = $this->input->cookie('language');
if (!isset($language))
{
  $language = $core_settings->language;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta name="Author" content=""/> 
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <link rel="stylesheet" href="invoice.css" type="text/css" charset="utf-8" />

    <style type="text/css">
      @page {
        margin: 0;
      }
      .pages {
        margin: .5in;
      }
      .other-pages{
        padding:60px;
      }
      .first-page {
        margin: 0in;
        background-color: black;
        height: 100%;
        width: 100%;
        position:absolute;
        background-image: url('https://www.example.com/wp-content/uploads/2015/09/example-cover1.jpg'); 
        background-position: bottom center; 
        background-repeat: no-repeat; 
        background-size: 100%;

      }
     .first-page + * {
        page-break-before: always;
      }


    </style>
  </head>
  <body>
    <div class="pages first-page"></div>
    <div class="other-pages">
        <h2>Title</h2>
        <div class="round"> 
        <table id="table" class="tablesorter" cellspacing="0"> 
            <thead> 
            <tr class="header"> 
              <th width="5%">#</th>
              <th width="95%">Tarea</th>
            </tr> 
            </thead> 
            <tbody> 
            <?php $i = 0; $sum = 0; $row=false; ?>
              <?php foreach ($tasks as $task):?>
                <tr <?php if($row){?>class="even"<?php } ?>>
                  <td><?php echo $i; ?></td>
                  <td><?php echo $task->name; ?><br><?php echo strip_tags($task->description); ?></td>
                </tr> 
              <?php $i++; endforeach;  $row = true; ?>
            </tbody> 
        </table> 
        </div> 
    </div>
  </body>
</html>

When I generate the PDF I have like this:

  1. COVER PAGE (without margin)
  2. TABLE WITH DYNAMIC CONTENT
  3. FULL BLANK PAGE (The problem)
  4. TABLE WITH DYNAMIC CONTENT (it's the same table always)

How I can fix the blank page?

NOTE: The content of my table depends of the content, so the height depends of the content, I cannot supose X fields and break page.


回答1:


When dompdf splits content across pages it duplicates the styling on the element. That means the following style is copied to the split element, causing a page break:

 .first-page + * {
    page-break-before: always;
  }

Since you just want a page break after the first page use the following instead:

 .first-page {
    page-break-after: always;
  }

At least one issue has been logged related to this problem: "doubling" of page breaks when page-break-before applied to long element



来源:https://stackoverflow.com/questions/32854869/how-i-can-break-my-document-correctly-with-dompdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!