我正在使用 dompdf HTML 到 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'> </td>
<td width='30'><center>X</center></td>
<td width='30'> </td>
<td width='30'> </td>
<td>Testing 12</td>
</tr>
<tr>
<td width='30'> </td>
<td width='30'><center>X</center></td>
<td width='30'> </td>
<td width='30'> </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'> </td>
<td width='30'><center>X</center></td>
<td width='30'> </td>
<td width='30'> </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'> </td>
<td width='30'><center>X</center></td>
<td width='30'> </td>
<td width='30'> </td>
<td>Testing 4</td>
</tr>
<tr>
<td width='30'> </td>
<td width='30'><center>X</center></td>
<td width='30'> </td>
<td width='30'> </td>
<td>Testing 5</td>
</tr>
<tr>
<td width='30'> </td>
<td width='30'><center>X</center></td>
<td width='30'> </td>
<td width='30'> </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'> </td>
<td width='30'><center>X</center></td>
<td width='30'> </td>
<td width='30'> </td>
<td>TESTING 7</td>
</tr>
<tr>
<td width='30'> </td>
<td width='30'><center>X</center></td>
<td width='30'> </td>
<td width='30'> </td>
<td>Testing 8</td>
</tr>
<tr>
<td width='30'> </td>
<td width='30'><center>X</center></td>
<td width='30'> </td>
<td width='30'> </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'> </td>
<td width='30'><center>X</center></td>
<td width='30'> </td>
<td width='30'> </td>
<td>Testing 10</td>
</tr>
<tr>
<td width='30'> </td>
<td width='30'><center>X</center></td>
<td width='30'> </td>
<td width='30'> </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'> </td>
<td width='30'><center>X</center></td>
<td width='30'> </td>
<td width='30'> </td>
<td>Testing 12</td>
</tr>
<tr>
<td width='30'> </td>
<td width='30'><center>X</center></td>
<td width='30'> </td>
<td width='30'> </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'> </td>
<td width='30'><center>X</center></td>
<td width='30'> </td>
<td width='30'> </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>