【发布时间】:2020-10-03 08:45:39
【问题描述】:
我正在使用 TCPDF 从 php 文件创建 PDF 文件。一切正常。现在我想使用来自服务器的现有 .pdf 文件添加一个附加页面。
最好的方法是使用 FPDI afaik。
但我找不到任何有关如何在 TCPDF 中设置 FPDI 以添加页面的文档或工作示例。我所看到的只是我如何使用外部 pdf 作为标题或背景等。
喜欢这个 https://www.setasign.com/products/fpdi/about/
我在 TCPDF 中的内容:
use setasign\Fpdi\Tcpdf\Fpdi;
// require_once('tcpdf/config/lang/eng.php');
require_once('TCPDF-main/tcpdf.php');
require_once('FPDI/src/autoload.php');
// Extend the TCPDF class to create custom Footer
class MYPDF extends TCPDF {
// Page footer
public function Footer() {
// Position at 15 mm from bottom
$this->SetY(-15);
// Set font
$this->SetFont('helvetica', 'I', 8);
// Page number
$this->Cell(0, 10, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
}
}
// add external PDF with FPDI (not working)
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', true);
$pageCount = $pdf->setSourceFile("hiking.pdf");
$tplIdx = $pdf->importPage(1, '/flyer');
$pdf->addPage();
$pdf->useTemplate($tplIdx, 10, 10, 90);
$pdf->Output();
// create new PDF document with TCPDF (working)
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', true);
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(true);
$pdf->AddPage();
ob_start(); //Start new output buffer
//Write page 1
$html = include('mytour.php');
$html = ob_get_contents();
$pdf->writeHTML($html, true, 0, true, 0);
// reset pointer to the last page
$pdf->lastPage();
ob_end_clean();
// ---------------------------------------------------------
$cdate = date("y-d-m");
$ctime = date("H-i-s");
//Close and output PDF document
$output1 = "mytour.pdf";
$pdf->Output($output1, 'I');
我收到错误: PHP 致命错误:未捕获的错误:调用未定义的方法 MYPDF::setSourceFile()
感谢任何有关如何操作的提示。
【问题讨论】: