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_once('tfpdf/tfpdf.php');
require_once('fpdi2/src/autoload.php');

class Pdf extends \setasign\Fpdi\Tfpdf\Fpdi
{
    /**
     * "Remembers" the template id of the imported page
     */
    protected $tplId;

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

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

        $text = 'tFPDF (v' . tFPDF::VERSION . ') and FPDI (v'
            . \setasign\Fpdi\Tfpdf\Fpdi::VERSION . ')';
        $this->Cell(0, $size['height'], $text);
        $this->Ln();
    }
}

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

// 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('tfpdf/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('I', 'generated.pdf');