PHP 转 PDF 分页
我正在使用 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>
I'm using the dompdf HTML to PDF converter to dynamically generate PDF files based on certain variables. I have the script creating a PHP file, and then reading from that PHP file to prompt the download of a PDF file using dompdf.
My only problem is that the output doesn't really care when it breaks the page in the PDF. For example, if I have a table towards the end of page 1, rather than moving the remainder of page 1 to page 2, it will cut the table in half and put one half on page 1 and one half on page 2.
I want to avoid this, and have the script truncate the PDF page so elements are succinctly aligned throughout the document.
Is there a way to do this in CSS, PHP, or some other way?
Here's the script that is outputting the data to a PHP file:
$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);
Pure HTML output:
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将其添加到您的 CSS 中可以修复它:
Adding this to your CSS fixes it for me: