Import pages from existing PDF documents and use them as templates in FPDF

FPDI

Import pages from existing PDF documents and use them as templates in FPDF

FPDI and tFPDF

This example shows how FPDI works together with tFPDF. tFPDF is a version of FPDF, which supports UTF-8 and font-subsetting.

PHP
<?php
// require tFPDF
require_once('tfpdf.php');

// map FPDF to tFPDF so FPDF_TPL can extend it
class FPDF extends tFPDF
{
    /**
     * "Remembers" the template id of the imported page
     */
    protected $_tplIdx;

    /**
     * Draw an imported PDF logo on every page
     */
    public function Header()
    {
        if (is_null($this->_tplIdx)) {
            $this->setSourceFile('logo.pdf');
            $this->_tplIdx = $this->importPage(1);
        }
        $size = $this->useTemplate($this->_tplIdx, 130, 5, 60);

        $this->SetFont('DejaVu', 'B', 16);
        $this->SetTextColor(0);
        $this->SetXY($this->lMargin, 5);

        $text = 'tFPDF (v' . tFPDF_VERSION . ') and FPDI (v'
            . FPDI::VERSION . ')';
        $this->Cell(0, $size['h'], $text);
        $this->Ln();
    }
}

// just require FPDI afterwards
require_once('fpdi.php');

// initiate PDF
$pdf = new FPDI();

// Add some Unicode font (uses UTF-8)
$pdf->AddFont('DejaVuSansCondensed', '', 'DejaVuSansCondensed.ttf', true);
$pdf->AddFont('DejaVuSansCondensed', 'B', 'DejaVuSansCondensed-Bold.ttf', true);

// add a page
$pdf->AddPage();

$pdf->SetFont('DejaVuSansCondensed', '', 14);

// Load a UTF-8 string from a file and print it
$txt = file_get_contents('HelloWorld.txt', true);
$pdf->Write(8, $txt);

// Select a standard font (uses windows-1252)
$pdf->SetFont('Arial', '', 14);
$pdf->Ln(10);
$pdf->Write(5, 'The file uses font subsetting.');

$pdf->Output();