【问题标题】:Need help on FPDI & TCPDF: page 1 of output pdf will use the first page of the template, the next pages will use the second page of the template在 FPDI 和 TCPDF 上需要帮助:输出 pdf 的第 1 页将使用模板的第一页,下一页将使用模板的第二页
【发布时间】:2016-01-02 12:43:25
【问题描述】:

我正在尝试创建发票文档并以 pdf 格式显示项目。

我遇到的问题是第一页将使用带有公司徽标、公司信息和购买物品清单的 pdf 模板(使用 FPDI)。

如果项目列表需要超过一页,则下一页将需要使用仅显示公司徽标作为标题的 pdf 模板的第二页以及所购买项目的延续。下面是我的示例代码。我还尝试基于此SO post 扩展 fpdi,但我无法使其工作。

    $pdf        = new FPDI();

    ob_start();
    echo $this->view->print_invoice($productItems, $customerInfo);
    $content    = ob_get_contents();
    ob_clean();

    // add a page
    $pdf->addPage();
    $pdf->writeHTML($content);

    $pageCount  = $pdf->setSourceFile(ROOT_DIR . "public/pdf-templates/group.pdf");
    $tplIdx     = $pdf->importPage(1, '/MediaBox');

    $pdf->useTemplate($tplIdx, 10, 10, 190, 280);

    $pdf->Output('outfile.pdf','I');

提前感谢您的帮助!

【问题讨论】:

    标签: php tcpdf fpdi


    【解决方案1】:

    只需实现你在扩展类的 header() 方法中描述的逻辑:

    class PDF extends FPDI
    {
        public function Header()
        {
            if ($this->PageNo() > 1) {
                $tplIdx = $this->importPage(2);
            } else {
                $tplIdx = $this->importPage(1);            
            }
    
            $this->useTemplate($tplIdx);
        }
    }
    
    ob_start();
    echo $this->view->print_invoice($productItems, $customerInfo);
    $content = ob_get_contents();
    ob_clean();
    
    $pdf = new PDF();
    $pdf->setSourceFile(ROOT_DIR . "public/pdf-templates/group.pdf");
    $pdf->addPage();
    $pdf->writeHTML($content);
    $pdf->Output('outfile.pdf','I');
    

    【讨论】:

    • 完美!谢谢你:)
    猜你喜欢
    • 2019-09-26
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 2021-03-24
    • 2013-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多