PHP到PDF分页

时间:2011-02-02 22:11:31

标签: php html css pdf dompdf

我正在使用dompdf HTML to PDF转换器根据某些变量动态生成PDF文件。我有脚本创建一个PHP文件,然后从该PHP文件读取,以提示使用dompdf下载PDF文件。

我唯一的问题是输出在打破PDF中的页面时并不在意。例如,如果我在第1页的末尾有一个表格,而不是将第1页的剩余部分移动到第2页,它会将表格缩小一半,将一半放在第1页,将一半放在第2页。

我想避免这种情况,并让脚本截断PDF页面,以便在整个文档中简洁地对齐元素。

有没有办法用CSS,PHP或其他方式做到这一点?

这是将数据输出到PHP文件的脚本:

 $filename = 'dompdf/www/test/' . $_GET['rubric'] . '_' . $_GET['student'] . '.php';
          $somecontent = "
<html><head>
<style type='text/css'>
html,body {
  font-family: 'myriad-pro-1','myriad-pro-2',Helvetica, Arial, Sans-Serif;
  height:100%;
  width:100%;
  overflow:auto;
  margin-left: 20px;
  margin-right: 20px;
}


     P.breakhere {page-break-before: always}

table
{
    border-color: #000;
    border-width: 1px;
    border-style: solid;
  border-collapse:collapse;
}

td
{
    border-color: #000;
    border-width: 1px 1px 0 0;
    border-style: solid;
}

#header {background:#ffffff url('gradient.png') no-repeat center center;
height: 100px;

}

#text {
position:relative;
text-align:center;
}

</style>
</head>
<body>


<div id='header'><div id='text'><h3>Upper School<br />";

".$rubricname."</h3></div></div>
Student Name: " . $student . "<br />
Division/Grade: " . $grade . "<br />
Teacher: " . $tname . "<br />
Date: " . $trow['due_date'] . "<br /><br />";

Habit 1: " . $1descrip . " <br />
<table width='100%'>
 <tr>
   <td>E</td>
   <td>M</td>
  <td>A</td>
   <td>D</td>
   <td>Skills and Content</td>
 </tr>

 <tr>
   <td width='30'>" . $grade_exceeds . "</td>
  <td width='30'>" . $grade_meets . "</td>
    <td width='30'>" . $grade_approaches . "</td>
      <td width='30'>" . $grade_doesnotmeet . "</td>
        <td>" . $crow['skill'] . "</td>
 </tr>


Habit 2: " . $2descrip . " <br />
<table width='100%'>
 <tr>
   <td>E</td>
   <td>M</td>
  <td>A</td>
   <td>D</td>
   <td>Skills and Content</td>
 </tr>

 <tr>
   <td width='30'>" . $grade_exceeds . "</td>
   <td width='30'>" . $grade_meets . "</td>
    <td width='30'>" . $grade_approaches . "</td>
     <td width='30'>" . $grade_doesnotmeet . "</td>
        <td>" . $crow['skill'] . "</td>
 </tr>

Habit 3: " . $3descrip . " <br />
<table width='100%'>
 <tr>
   <td>E</td>
   <td>M</td>
  <td>A</td>
   <td>D</td>
   <td>Skills and Content</td>
 </tr>

 <tr>
   <td width='30'>" . $grade_exceeds . "</td>
 <td width='30'>" . $grade_meets . "</td>
    <td width='30'>" . $grade_approaches . "</td>
      <td width='30'>" . $grade_doesnotmeet . "</td>
        <td>" . $crow['skill'] . "</td>
 </tr>

Habit 4: " . $4descrip . " <br />
<table width='100%'>
 <tr>
   <td>E</td>
   <td>M</td>
  <td>A</td>
   <td>D</td>
   <td>Skills and Content</td>
 </tr>

 <tr>
   <td width='30'>" . $grade_exceeds . "</td>
  <td width='30'>" . $grade_meets . "</td>
    <td width='30'>" . $grade_approaches . "</td>
      <td width='30'>" . $grade_doesnotmeet . "</td>
        <td>" . $crow['skill'] . "</td>
 </tr>

Habit 5: " . $5descrip . " <br />
<table width='100%'>
 <tr>
   <td>E</td>
   <td>M</td>
  <td>A</td>
   <td>D</td>
   <td>Skills and Content</td>
 </tr>

 <tr>
   <td width='30'>" . $grade_exceeds . "</td>
 <td width='30'>" . $grade_meets . "</td>
    <td width='30'>" . $grade_approaches . "</td>
      <td width='30'>" . $grade_doesnotmeet . "</td>
        <td>" . $crow['skill'] . "</td>
 </tr>

Habit 6: " . $6descrip . " <br />
<table width='100%'>
 <tr>
   <td>E</td>
   <td>M</td>
  <td>A</td>
   <td>D</td>
   <td>Skills and Content</td>
 </tr>

 <tr>
   <td width='30'>" . $grade_exceeds . "</td>
  <td width='30'>" . $grade_meets . "</td>
    <td width='30'>" . $grade_approaches . "</td>
     <td width='30'>" . $grade_doesnotmeet . "</td>
        <td>" . $crow['skill'] . "</td>
 </tr>

Habit 7: " . $7descrip . " <br />
<table width='100%'>
 <tr>
   <td>E</td>
   <td>M</td>
  <td>A</td>
   <td>D</td>
   <td>Skills and Content</td>
 </tr>

 <tr>
   <td width='30'>" . $grade_exceeds . "</td>
  <td width='30'>" . $grade_meets . "</td>
    <td width='30'>" . $grade_approaches . "</td>
      <td width='30'>" . $grade_doesnotmeet . "</td>
        <td>" . $crow['skill'] . "</td>
 </tr>


<font size='+1'>Comments</font><br />

</body></html>";


          // Let's make sure the file exists and is writable first.
          // In our example we're opening $filename in append mode.
          // The file pointer is at the bottom of the file hence
          // that's where $somecontent will go when we fwrite() it.
          if (!$handle = fopen($filename, 'w+')) {
              echo "Cannot open file ($filename)";
              exit;
          } //if (!$handle = fopen($filename, 'w+'))
          // Write $somecontent to our opened file.
          if (fwrite($handle, $somecontent) === false) {
              echo "Cannot write to file ($filename)";
              exit;
          } //if (fwrite($handle, $somecontent) === false)
          //echo "Success, wrote ($somecontent) to file ($filename)";
          //echo "Success!";
          fclose($handle);

纯HTML输出:

<html><head>
<style type='text/css'>
html,body {
    font-family: 'myriad-pro-1','myriad-pro-2',Helvetica, Arial, Sans-Serif;
    height:100%;
    width:100%;
    overflow:auto;
    margin-left: 20px;
    margin-right: 20px;
}


     P.breakhere {page-break-before: always}

table
{
    border-color: #000;
    border-width: 1px;
    border-style: solid;
    border-collapse:collapse;
}

td
{
    border-color: #000;
    border-width: 1px 1px 0 0;
    border-style: solid;
}

#header {background:#ffffff url('gradient.png') no-repeat center center;
height: 100px;

}

#text {
position:relative;
text-align:center;
}

</style>
</head>
<body>


<div id='header'><div id='text'><h3>Upper School<br />Spanish 3<br />
Testing Rubric</h3></div></div>
Student Name: [name]<br />
Division/Grade: Senior Institute/Grade 11<br />

Teacher: [name]<br />
Date: 2/1/2011<br /><br />

Habit Name: test22222 <br />
<table width='100%'>
 <tr>
   <td>E</td>
   <td>M</td>
  <td>A</td>

   <td>D</td>
   <td>Skills and Content</td>
 </tr>

 <tr>
   <td width='30'>&nbsp;</td>
  <td width='30'><center>X</center></td>
    <td width='30'>&nbsp;</td>

      <td width='30'>&nbsp;</td>
        <td>Testing 12</td>
 </tr>


 <tr>
   <td width='30'>&nbsp;</td>
  <td width='30'><center>X</center></td>
    <td width='30'>&nbsp;</td>

      <td width='30'>&nbsp;</td>
        <td>Testing 2</td>
 </tr>

[name] is approaching standards for the Habit Name. </table><br />
Habit Name: tes <br />
<table width='100%'>
 <tr>
   <td>E</td>

   <td>M</td>
  <td>A</td>
   <td>D</td>
   <td>Skills and Content</td>
 </tr>

 <tr>

   <td width='30'>&nbsp;</td>
   <td width='30'><center>X</center></td>
    <td width='30'>&nbsp;</td>
     <td width='30'>&nbsp;</td>
        <td>Testing 3</td>
 </tr>
[name] is not meeting standards for the Habit Name. </table><br />
Habit Name: test22222 <br />

<table width='100%'>
 <tr>
   <td>E</td>
   <td>M</td>
  <td>A</td>
   <td>D</td>
   <td>Skills and Content</td>

 </tr>

 <tr>
   <td width='30'>&nbsp;</td>
 <td width='30'><center>X</center></td>
    <td width='30'>&nbsp;</td>
      <td width='30'>&nbsp;</td>
        <td>Testing 4</td>

 </tr>

 <tr>
   <td width='30'>&nbsp;</td>
 <td width='30'><center>X</center></td>
    <td width='30'>&nbsp;</td>
      <td width='30'>&nbsp;</td>
        <td>Testing 5</td>

 </tr>

 <tr>
   <td width='30'>&nbsp;</td>
 <td width='30'><center>X</center></td>
    <td width='30'>&nbsp;</td>
      <td width='30'>&nbsp;</td>
        <td>Testing 6</td>

 </tr>
[name] is meeting standards for the Habit Name. </table><br />
Habit Name: test22222 <br />
<table width='100%'>
 <tr>
   <td>E</td>
   <td>M</td>
  <td>A</td>

   <td>D</td>
   <td>Skills and Content</td>
 </tr>

 <tr>
   <td width='30'>&nbsp;</td>
  <td width='30'><center>X</center></td>
    <td width='30'>&nbsp;</td>

      <td width='30'>&nbsp;</td>
        <td>TESTING 7</td>
 </tr>

 <tr>
   <td width='30'>&nbsp;</td>
  <td width='30'><center>X</center></td>
    <td width='30'>&nbsp;</td>

      <td width='30'>&nbsp;</td>
        <td>Testing 8</td>
 </tr>

 <tr>
   <td width='30'>&nbsp;</td>
  <td width='30'><center>X</center></td>
    <td width='30'>&nbsp;</td>

      <td width='30'>&nbsp;</td>
        <td>Testing 9</td>
 </tr>
[name] is exceeding standards for the Habit Name. </table><br />
Habit Name: test22222 <br />
<table width='100%'>
 <tr>
   <td>E</td>

   <td>M</td>
  <td>A</td>
   <td>D</td>
   <td>Skills and Content</td>
 </tr>

 <tr>

   <td width='30'>&nbsp;</td>
 <td width='30'><center>X</center></td>
    <td width='30'>&nbsp;</td>
      <td width='30'>&nbsp;</td>
        <td>Testing 10</td>
 </tr>

 <tr>

   <td width='30'>&nbsp;</td>
 <td width='30'><center>X</center></td>
    <td width='30'>&nbsp;</td>
      <td width='30'>&nbsp;</td>
        <td>Testing 11</td>
 </tr>
[name] is exceeding standards for the Habit Name. </table><br />
Habit Name: test22222 <br />

<table width='100%'>
 <tr>
   <td>E</td>
   <td>M</td>
  <td>A</td>
   <td>D</td>
   <td>Skills and Content</td>

 </tr>

 <tr>
   <td width='30'>&nbsp;</td>
  <td width='30'><center>X</center></td>
    <td width='30'>&nbsp;</td>
     <td width='30'>&nbsp;</td>
        <td>Testing 12</td>

 </tr>

 <tr>
   <td width='30'>&nbsp;</td>
  <td width='30'><center>X</center></td>
    <td width='30'>&nbsp;</td>
     <td width='30'>&nbsp;</td>
        <td>Testing 13</td>

 </tr>
[name] is exceeding standards for the Habit Name. </table><br />

Habit Name: test22222 <br />
<table width='100%'>
 <tr>
   <td>E</td>
   <td>M</td>
  <td>A</td>

   <td>D</td>
   <td>Skills and Content</td>
 </tr>

 <tr>
   <td width='30'>&nbsp;</td>
  <td width='30'><center>X</center></td>
    <td width='30'>&nbsp;</td>

      <td width='30'>&nbsp;</td>
        <td>Testing 14</td>
 </tr>
[name] is exceeding standards for the Habit Name. </table>
<P CLASS='breakhere'>

<font size='+1'>Comments</font><br />This is testing a large comment. 

</body></html>

1 个答案:

答案 0 :(得分:3)

将此添加到CSS中为我修复了它:

table {
    page-break-inside: avoid
}

<html>
<head>
<style type='text/css'>
html, body {
    font-family: 'myriad-pro-1', 'myriad-pro-2', Helvetica, Arial, Sans-Serif;
    margin-left: 20px;
    margin-right: 20px;
}
p.breakhere {
    page-break-before: always
}
table {
    border-collapse: collapse;
    page-break-inside: avoid
}
td {
    border: 1px solid #000
}
.noBorder {
    border: 0
}
#header {
    background:#ffffff url('gradient.png') no-repeat center center;
    height: 100px;
}
#text {
    position:relative;
    text-align:center;
}
</style>
</head>
<body>
<div id='header'>
    <div id='text'>
        <h3>Upper School<br />
            Spanish 3<br />
            Testing Rubric</h3>
    </div>
</div>
Student Name: [name]<br />
Division/Grade: Senior Institute/Grade 11<br />
Teacher: [name]<br />
Date: 2/1/2011<br />
<br />
<table width='100%'>
    <tr>
        <td>E</td>
        <td>M</td>
        <td>A</td>
        <td>D</td>
        <td>Skills and Content</td>
    </tr>
    <tr>
        <td width='30'>&nbsp;</td>
        <td width='30'><center>
                X
            </center></td>
        <td width='30'>&nbsp;</td>
        <td width='30'>&nbsp;</td>
        <td>Testing 12</td>
    </tr>
    <tr>
        <td width='30'>&nbsp;</td>
        <td width='30'><center>
                X
            </center></td>
        <td width='30'>&nbsp;</td>
        <td width='30'>&nbsp;</td>
        <td>Testing 2</td>
    </tr>
    <tr>
        <td width='30'>&nbsp;</td>
        <td width='30'><center>
                X
            </center></td>
        <td width='30'>&nbsp;</td>
        <td width='30'>&nbsp;</td>
        <td>Testing 2</td>
    </tr>
        <tr>
        <td width='30'>&nbsp;</td>
        <td width='30'><center>
                X
            </center></td>
        <td width='30'>&nbsp;</td>
        <td width='30'>&nbsp;</td>
        <td>Testing 2</td>
    </tr>
        <tr>
        <td width='30'>&nbsp;</td>
        <td width='30'><center>
                X
            </center></td>
        <td width='30'>&nbsp;</td>
        <td width='30'>&nbsp;</td>
        <td>Testing 2</td>
    </tr>
    <tr>
        <td colspan="5" class="noBorder">[name] is approaching standards for the Habit Name.</td>
    </tr>
</table>
<span style="color: #fff">-</span>

<table width='100%'>
    <tr>
        <td>E</td>
        <td>M</td>
        <td>A</td>
        <td>D</td>
        <td>Skills and Content</td>
    </tr>
    <tr>
        <td width='30'>&nbsp;</td>
        <td width='30'><center>
                X
            </center></td>
        <td width='30'>&nbsp;</td>
        <td width='30'>&nbsp;</td>
        <td>Testing 12</td>
    </tr>
    <tr>
        <td width='30'>&nbsp;</td>
        <td width='30'><center>
                X
            </center></td>
        <td width='30'>&nbsp;</td>
        <td width='30'>&nbsp;</td>
        <td>Testing 2</td>
    </tr>
    <tr>
        <td width='30'>&nbsp;</td>
        <td width='30'><center>
                X
            </center></td>
        <td width='30'>&nbsp;</td>
        <td width='30'>&nbsp;</td>
        <td>Testing 2</td>
    </tr>
        <tr>
        <td width='30'>&nbsp;</td>
        <td width='30'><center>
                X
            </center></td>
        <td width='30'>&nbsp;</td>
        <td width='30'>&nbsp;</td>
        <td>Testing 2</td>
    </tr>
        <tr>
        <td width='30'>&nbsp;</td>
        <td width='30'><center>
                X
            </center></td>
        <td width='30'>&nbsp;</td>
        <td width='30'>&nbsp;</td>
        <td>Testing 2</td>
    </tr>
    <tr>
        <td colspan="5" class="noBorder">[name] is approaching standards for the Habit Name.</td>
    </tr>
</table>
<span style="color: #fff">-</span>

<table width='100%'>
    <tr>
        <td>E</td>
        <td>M</td>
        <td>A</td>
        <td>D</td>
        <td>Skills and Content</td>
    </tr>
    <tr>
        <td width='30'>&nbsp;</td>
        <td width='30'><center>
                X
            </center></td>
        <td width='30'>&nbsp;</td>
        <td width='30'>&nbsp;</td>
        <td>Testing 12</td>
    </tr>
    <tr>
        <td width='30'>&nbsp;</td>
        <td width='30'><center>
                X
            </center></td>
        <td width='30'>&nbsp;</td>
        <td width='30'>&nbsp;</td>
        <td>Testing 2</td>
    </tr>
    <tr>
        <td width='30'>&nbsp;</td>
        <td width='30'><center>
                X
            </center></td>
        <td width='30'>&nbsp;</td>
        <td width='30'>&nbsp;</td>
        <td>Testing 2</td>
    </tr>
        <tr>
        <td width='30'>&nbsp;</td>
        <td width='30'><center>
                X
            </center></td>
        <td width='30'>&nbsp;</td>
        <td width='30'>&nbsp;</td>
        <td>Testing 2</td>
    </tr>
        <tr>
        <td width='30'>&nbsp;</td>
        <td width='30'><center>
                X
            </center></td>
        <td width='30'>&nbsp;</td>
        <td width='30'>&nbsp;</td>
        <td>Testing 2</td>
    </tr>
    <tr>
        <td colspan="5" class="noBorder">[name] is approaching standards for the Habit Name.</td>
    </tr>
</table>
<span style="color: #fff">-</span>

<table width='100%'>
    <tr>
        <td>E</td>
        <td>M</td>
        <td>A</td>
        <td>D</td>
        <td>Skills and Content</td>
    </tr>
    <tr>
        <td width='30'>&nbsp;</td>
        <td width='30'><center>
                X
            </center></td>
        <td width='30'>&nbsp;</td>
        <td width='30'>&nbsp;</td>
        <td>Testing 12</td>
    </tr>
    <tr>
        <td width='30'>&nbsp;</td>
        <td width='30'><center>
                X
            </center></td>
        <td width='30'>&nbsp;</td>
        <td width='30'>&nbsp;</td>
        <td>Testing 2</td>
    </tr>
    <tr>
        <td width='30'>&nbsp;</td>
        <td width='30'><center>
                X
            </center></td>
        <td width='30'>&nbsp;</td>
        <td width='30'>&nbsp;</td>
        <td>Testing 2</td>
    </tr>
        <tr>
        <td width='30'>&nbsp;</td>
        <td width='30'><center>
                X
            </center></td>
        <td width='30'>&nbsp;</td>
        <td width='30'>&nbsp;</td>
        <td>Testing 2</td>
    </tr>
        <tr>
        <td width='30'>&nbsp;</td>
        <td width='30'><center>
                X
            </center></td>
        <td width='30'>&nbsp;</td>
        <td width='30'>&nbsp;</td>
        <td>Testing 2</td>
    </tr>
            <tr>
        <td width='30'>&nbsp;</td>
        <td width='30'><center>
                X
            </center></td>
        <td width='30'>&nbsp;</td>
        <td width='30'>&nbsp;</td>
        <td>Testing 2</td>
    </tr>
            <tr>
        <td width='30'>&nbsp;</td>
        <td width='30'><center>
                X
            </center></td>
        <td width='30'>&nbsp;</td>
        <td width='30'>&nbsp;</td>
        <td>Testing 2</td>
    </tr>
            <tr>
        <td width='30'>&nbsp;</td>
        <td width='30'><center>
                X
            </center></td>
        <td width='30'>&nbsp;</td>
        <td width='30'>&nbsp;</td>
        <td>Testing 2</td>
    </tr>
    <tr>
        <td colspan="5" class="noBorder">[name] is approaching standards for the Habit Name.</td>
    </tr>
</table>
<span style="color: #fff">-</span>

<table width='100%'>
    <tr>
        <td>E</td>
        <td>M</td>
        <td>A</td>
        <td>D</td>
        <td>Skills and Content</td>
    </tr>
    <tr>
        <td width='30'>&nbsp;</td>
        <td width='30'><center>
                X
            </center></td>
        <td width='30'>&nbsp;</td>
        <td width='30'>&nbsp;</td>
        <td>Testing 12</td>
    </tr>
    <tr>
        <td width='30'>&nbsp;</td>
        <td width='30'><center>
                X
            </center></td>
        <td width='30'>&nbsp;</td>
        <td width='30'>&nbsp;</td>
        <td>Testing 2</td>
    </tr>
    <tr>
        <td width='30'>&nbsp;</td>
        <td width='30'><center>
                X
            </center></td>
        <td width='30'>&nbsp;</td>
        <td width='30'>&nbsp;</td>
        <td>Testing 2</td>
    </tr>
        <tr>
        <td width='30'>&nbsp;</td>
        <td width='30'><center>
                X
            </center></td>
        <td width='30'>&nbsp;</td>
        <td width='30'>&nbsp;</td>
        <td>Testing 2</td>
    </tr>
        <tr>
        <td width='30'>&nbsp;</td>
        <td width='30'><center>
                X
            </center></td>
        <td width='30'>&nbsp;</td>
        <td width='30'>&nbsp;</td>
        <td>Testing 2</td>
    </tr>
    <tr>
        <td colspan="5" class="noBorder">[name] is approaching standards for the Habit Name.</td>
    </tr>
</table>
<span style="color: #fff">-</span>



<font size='+1'>Comments</font><br />
This is testing a large comment.
</body>
</html>