【问题标题】:How to create pdf file in loop in cakephp?如何在cakephp中循环创建pdf文件?
【发布时间】:2012-02-21 11:59:51
【问题描述】:

我已经实现了 pdf 创建,

http://bakery.cakephp.org/articles/kalileo/2010/06/08/creating-pdf-files-with-cakephp-and-tcpdf

但按照我的逻辑, 有一个链接“printAll”然后在循环中我必须多次生成发票pdf。 那么是否可以使用循环在一个函数中创建多个 pdf 文件?

到目前为止,我已将逻辑放入 ctp 文件并在 foreach 循环中写入逻辑,但仍然只生成第一个记录的 pdf。

我该怎么做?有没有其他方法可以做到这一点?

所以请任何人都可以帮助我??

提前谢谢..

在 generate_invoice.ctp 中


<?php
     App::import('Vendor','xtcpdf'); 
     $tcpdf = new XTCPDF();
     $textfont = 'freesans'; 
     $tcpdf->SetAutoPageBreak( false );
     $tcpdf->xheadercolor = array(255,255,255);
     $tcpdf->AddPage();
     $tcpdf->SetTextColor(0, 0, 0);
     $tcpdf->SetFont($textfont,'',10);
     $total_records = 0;
     foreach($POdata as $order)
 {
         $po = $order['Orderproduct']['id'];
            $total_records = $total_records + 1;
 }
     if($total_records == 1)
     {  
     $html = <<<EOD
         $po; //and other html 
         EOD;
        $tcpdf->writeHTMLCell(0, 0, '', '', $html,'', 1, 0, true, '', true);
        echo $tcpdf->Output('filename.pdf', 'D');                                                   
    }
    else if($total_records > 1) 
    {
        foreach($POdata as $order)
        {   
                $po = $order['Orderproduct']['id'];
            $total_records = $total_records + 1;
            $html = <<<EOD
                   $po; //and other html 
            EOD;
            $tcpdf->writeHTMLCell(0, 0, '', '', $html,'', 1, 0, true, '', true);
           $filename = "PO".$poid.".pdf";
    echo $tcpdf->Output($filename, 'D');
            }
     }

并且从控制器文件到视图文件的数据在 $POdata 中正确获取。 这么新,你能帮帮我吗?

【问题讨论】:

    标签: cakephp


    【解决方案1】:

    是的,我们可以做到这一点

    使用 foreach() 重复对象,并根据该数据生成 PDF 并存储在某个位置,您可以制作每个文件的显示路径数组

    将此数组传递给视图对象,您可以在视图文件中使用它,也可以将其设置为下载链接

    谢谢

    【讨论】:

    • 只需剪切我代码的前 8 行并粘贴到 'if' 和 'else' 块中,就在 '$html' 语句的上方,就完成了......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多