【问题标题】:Loop in creating mutliple PDF in DOMPDF在 DOMPDF 中循环创建多个 PDF
【发布时间】:2016-06-02 06:44:18
【问题描述】:

我想在其中循环我的 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">

【问题讨论】:

  • 找到任何东西 dhh

标签: php pdf dompdf


【解决方案1】:

首先查看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">

【讨论】:

    猜你喜欢
    • 2014-04-17
    • 1970-01-01
    • 2020-05-24
    • 2017-08-19
    • 1970-01-01
    • 1970-01-01
    • 2015-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多