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