【问题标题】:FPDF FPDI How to import a page from different FPDF class?FPDF FPDI 如何从不同的 FPDF 类导入页面?
【发布时间】:2018-07-28 21:01:03
【问题描述】:

我有一个创建 FPDF 文档的类。我想将该文档包含在不同的 FPDF 类中。

    // Document/Class 1
    $pdf->new MyFirstDocument();
    $pdf->output

    // Document/Class 2
    class MySecondDocument extends FPDF {

    $this->addPage() //etc

    //and from here i would like to call the 
    //class MyFirstDocument and import the output 
    //into MySecondDocument as an additional page

    }

【问题讨论】:

  • 在创建“MySecondDocument”时尝试扩展“MyFirstDocument”类而不是扩展FPDF,例如class MySecondDocument extends MyFirstDocument。然后一次调用所有需要的函数,然后调用输出。
  • 我想过这一点,但想法是可以根据情况添加几个额外的页面。因此,我将创建 MySecondDocument,比如说创建 2 个通用页面,然后根据情况可能会有另外一个或两个其他页面,依此类推。我现在的解决方法是创建一个文件,保存它,使用 fpdi 导入它,然后删除该文件。但这似乎有点难看。
  • 您可以在一个类或页面中添加该条件,然后根据您的条件创建所有页面并在所有这些调用输出之后。您可以有条件地添加页面$this->addPage(),然后创建文档。

标签: php fpdf fpdi


【解决方案1】:

您写道:

我有一个创建 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();
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-24
    • 1970-01-01
    • 2011-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-05
    相关资源
    最近更新 更多