【问题标题】:How to use loop in creating multiple pdf file in dompdf如何使用循环在dompdf中创建多个pdf文件
【发布时间】:2014-12-17 06:53:39
【问题描述】:
<?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”。请帮助我。

【问题讨论】:

    标签: php dompdf


    【解决方案1】:

    试试这个:

    你的循环是错误的。您向 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();
    

    注意:您需要确定,更接近 HTML 的heredoc 是否在新行中并且没有缩进。

    【讨论】:

    • 它有效。非常感谢您的帮助。我现在明白了。
    • 好的接受。有没有其他方法可以创建循环而不一次又一次嵌入“$ html”。我的意思是当我尝试只显示一页pdf时,整个php代码在没有添加的情况下工作"$html" 。如何将总的php代码放入一个会产生多个pdf文件的循环中。任何解决方案???
    • 使用 .jpg 图像的完整目录路径以 pdf 格式显示图像
    • @MangeshParte 此代码生成了三个文件,但我如何保存这些 pdf 文件,因为 pdf 文件是在网络浏览器中打开的,而不是保存在计算机中。
    猜你喜欢
    • 1970-01-01
    • 2014-04-17
    • 2017-08-19
    • 1970-01-01
    • 2015-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-24
    相关资源
    最近更新 更多