如何使用循环在 dompdf 中创建多个 pdf 文件


How to use loop in creating multiple pdf file in dompdf

<?php
require_once 'classes/postgredb.class.php';
require_once 'include/functions.php';
require_once("/tools/dompdf/dompdf_config.inc.php");
$con=new PostgreDB(); 
ob_start();
$html =
    '<html><body>'.
    '<p>Hello World!</p>'.
    '<div style="page-break-after: always;"></div>'.
    '</body></html>';
for($i=0;$i<5;$i++)
{

$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("Admit card.pdf",array("Attachment"=>0));
}

?>

我想在每一页中打印"Hello World",但我的代码只在一页中打印它。如何使用循环在每页中打印"Hello World"。请帮助我。

试试这个:

你的循环是错误的。将页面添加到 PDF 的方式可能是错误的。显然,您正在一次又一次地覆盖一页,而不是附加新页面。

$html = <<<HTML
  <html>
      <head>
            <style type="text/css">
                /* Your document styling goes here */
            </style>
      </head>
      <body>
HTML;
for($i=0;$i<5;$i++)
{
    $html .= '<div style="page-break-after: always;"><p>Hello World!</p></div>';
}
$html .= '</body></html>';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("Admit card.pdf",array("Attachment"=>0));
$dompdf->clear();

注意:您需要确保 heredoc 更近HTML在新行中而不是缩进。