您写道:
我有一个创建 FPDF 文档的类。我想将该文档包含在不同的 FPDF 类中。
这是错误的!在$pdf->Output() 之后你不能有更多的输出,因为$pdf->Output() 创建了一个PDF 文档。每个 PDF 文档只能使用一次。请阅读documentation。
您也不能从 FPDF 获得第二个实例。因此,您必须在头等舱中使用 FPDF 实例作为参数的构造函数。
解决方案示例:
由于这一切,我们无法使用 FPDF 从不同的 FPDF 类中真正导入页面,但我们可以执行以下操作。
来自firstpdf_class.php的代码:
<?php
class FirstPDF_Class
{
private $fpdf_instance;
function __construct($fpdf_instance)
{
$this->fpdf_instance = $fpdf_instance;
}
function print_title($doc_title, $company)
{
$this->fpdf_instance->SetFont('Helvetica','B', 18);
$this->fpdf_instance->Cell(210,4, $company, 0, 0, 'C');
$this->fpdf_instance->Ln();
$this->fpdf_instance->Ln();
$this->fpdf_instance->Cell(37);
$this->fpdf_instance->SetFillColor(209, 204, 244);
$this->fpdf_instance->SetFont('Helvetica', 'B', 11);
$this->fpdf_instance->Cell(150,8, $doc_title, 0, 0, 'C', 1);
}
}
?>
来自secondpdf_class.php的代码:
<?php
require('fpdf.php');
require('firstpdf_class.php');
class SecondPDF_Class extends FPDF
{
private $printpdf;
function __construct($orientation = 'P', $unit = 'mm', $size = 'A4')
{
parent::__construct($orientation, $unit, $size);
$this->printpdf = new FirstPDF_Class($this);
$this->import_page('Document 1', 'Company "Fruits Sell"');
$this->import_page('Document 2', 'Company "Boot Sell"');
}
public function import_page($doc_title, $company)
{
$this->AddPage();
$this->printpdf->print_title($doc_title, $company);
}
function Footer()
{
$this->SetXY(100, -15);
$this->SetFont('Helvetica','I', 10);
$this->SetTextColor(128, 128,128);
// Page number
$this->Cell(0, 10,'This is the footer. Page '.$this->PageNo(),0,0,'C');
}
}
$pdf = new SecondPDF_Class();
//not really import page, but some like this
$pdf->import_page('Document 3', 'Company "Auto Sell"');
$pdf->AddPage();
$pdf->SetFont('Helvetica','B', 18);
$pdf->Cell(210,4, 'Page 3.', 0, 0, 'C');
$pdf->Output();
?>