Php FPDF FPDI如何从不同的FPDF类导入页面?

Php FPDF FPDI如何从不同的FPDF类导入页面?,php,fpdf,fpdi,Php,Fpdf,Fpdi,我有一个创建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 c

我有一个创建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

    }

您写道:

我有一个创建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

    }
这是错误的!您不能在
$pdf->Output()
之后再进行任何输出,因为
$pdf->Output()
会创建pdf文档。每个PDF文档只需使用一次。请仔细阅读

您也不能从FPDF获得第二个实例。因此,在第一个类中必须有一个构造函数,参数为来自FPDF的实例

解决方案示例:

由于所有这些原因,我们无法使用FPDF从不同的FPDF类导入页面,但我们可以执行如下操作

来自firstpdf\u class.php的代码:


代码来自secondpdf\u class.php


在创建“MySecondDocument”时,尝试扩展“MyFirstDocument”类,而不是扩展FPDF,例如
类MySecondDocument扩展MyFirstDocument
。然后一次调用所有所需的函数,然后调用输出。我考虑过这一点,但想法是,根据情况,可能会有多个附加页面。所以我有我的SecondDocument,它创建了2个常规页面,然后根据情况可能会有一个或两个其他页面,以此类推。我现在的工作是创建一个文件,保存它,用fpdi导入它,然后删除该文件。但这似乎有点难看。你可以在一个类或页面中添加这些条件,然后根据你的条件创建所有页面,并在所有这些调用之后输出。您可以有条件地添加页面
$this->addPage()
,然后创建文档。