我想在此循环我的PDF内容,目前我正在使用DOMPDF来创建PDF。在循环PDF内容时,我没有任何pdf内容。如果我删除循环,我得到pdf。实际上,我想要一个包含5页的PDF。
任何人都可以帮助我吗?在此先感谢。
<?php
include('config/config.php');
require_once('dompdf_config.inc.php');
error_reporting(0);
for ($i = 0; $i <= 5; $i++) {
//here is my content to loop
$pdf_content = '<body>
<table width="450" border="0" cellpadding="5" class="table_data">
<tr>
<td class="left_col">3. Chassis No</td>
<td><b>: ' . $chass_no . ' </b></td>
</tr>
<tr>
<td class="left_col">4. Engine No. </td>
<td> <b> : ' . $engine_id . ' </b></td>
</tr>
</table>
</body>
';
//create pdf function
function createPDF($pdf_userid, $pdf_content, $pdf_For, $filename)
{
$path = 'UsersActivityReports/';
$dompdf = new DOMPDF();
$dompdf->load_html($pdf_content);
$dompdf->render();
$output = $dompdf->output();
file_put_contents($path . $filename, $output);
file_get_contents('output1.php');
return $filename;
}
$name = date("d-m-y") . rand() . '.pdf';
$reportPDF = createPDF(12, $pdf_content, 'activity_Report', $name);
}
//loop ends here
?>
<iframe class="prv_pdf" src="/UsersActivityReports/<?php echo $name ?>#zoom=50" height="300px" width="440px">
答案 0 :(得分:0)
首先看this question。在循环内声明函数将在第二次遇到代码时导致错误。所以开始在循环之外移动。
其次,您在每个循环上生成一个新的PDF,并且每次都将该PDF保存到文件文件中。因此,您只能获得一页PDF。您可能想要做的是在每个循环上生成HTML内容,然后在完成后生成PDF。
尝试这样的事情(根据问题中的实际内容进行简化):
require_once('dompdf_config.inc.php');
function createPDF($pdf_content, $filename) {
$dompdf = new DOMPDF();
$dompdf->load_html($pdf_content);
$dompdf->render();
$output = $dompdf->output();
file_put_contents($filename, $output);
}
$pdf_content = '
<html>
<head>
<style>
.table_data { page-break-before: always; }
.table_data:first-child { page-break-before: never; }
</style>
</head>
<body>
';
for ($i = 0; $i <= 5; $i++) {
$pdf_content = '
<table width="450" border="0" cellpadding="5" class="table_data">
<tr>
<td class="left_col">3. Chassis No</td>
<td>: <b>' . $chass_no . '</b></td>
</tr>
<tr>
<td class="left_col">4. Engine No. </td>
<td>: <b>' . $engine_id . '</b></td>
</tr>
</table>
';
}
$html_content = '</body></html>';
$name = 'UsersActivityReports/' . date("d-m-y") . rand() . '.pdf';
createPDF($pdf_content, $name);
?>
<iframe class="prv_pdf" src="<?php echo $name ?>#zoom=50" height="300px" width="440px">