0

I have a class that creates a FPDF document. And I would like to include that document in a different FPDF class.

    // 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

    }
Bharata
  • 13,509
  • 6
  • 36
  • 50
  • Try extending the "MyFirstDocument" class instead extending FPDF when creating "MySecondDocument ", like this `class MySecondDocument extends MyFirstDocument`. Then call all the desired function at once and then call output. – Hello World Jul 28 '18 at 08:13
  • I thought about that, but the idea is that there could be several additional pages to include depending on the situation. So I'd have MySecondDocument which creates let's say 2 general pages and then depending on the situation there could be one other or two others and so on. My workround right now is to create a file, save it, import it with fpdi and then delete the file. but that seems a little bit ugly. – user3611673 Jul 28 '18 at 08:18
  • You can add that conditions in one class or page and then after creating all the pages depending on your conditions and after all these calls the output. You can add page `$this->addPage()` conditionally and then create the document. – Hello World Jul 28 '18 at 08:26

1 Answers1

0

You wrote:

I have a class that creates a FPDF document. And I would like to include that document in a different FPDF class.

This is wrong! You can not have after $pdf->Output() anything output more, because $pdf->Output() creates a PDF document. You have to use it only one time per PDF document. Please read the documentation.

You can not have a second instance from FPDF too. Because of this you have to have in first class a constructer with instance from FPDF as parameter.

Solution example:

Because of all this we can not really import page from different FPDF class with FPDF, but we could do some like follows.

Code from 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);
    }
}
?>

Code from 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();
?>
Bharata
  • 13,509
  • 6
  • 36
  • 50