The FpdfTpl Class An overview of the FpdfTpl class

Table of Contents

  1. Introduction
    1. Usage
      1. Methods

        Introduction

        The class FpdfTpl was build as an intermediate class between FPDF and FPDI in the past. In version 2 this class was implemented for backwards-compatibility. Also the logic of both classes were decoupled much more in FPDI 2.

        The class offers a possibility to reuse content within a PDF document through a technic called "form XObjects".

        You can see this technic as a kind of template to which you can write and reuse it several times without re-writing its content by only referencing to it.

        The class offers following advantages when using templates: 

        • Data of templates are included in a PDF document only once
        • Less memory usage
        • Less generation time
        • Smaller PDF files
        • Recursive template support (use of templates in templates)

        Examples of use (the greatest advantage of all is that a template has to be written to the PDF file only
        once):

        • Header generation for usage on several pages
        • Grids for large tables over more pages
        • Tableheaders
        • Write behind or in front of a template
        • Resize a template after creation
        • ...  

        FpdfTpl is available for FPDF through \setasign\Fpdi\FpdfTpl, for tFPDF through \setasign\Fpdi\Tfpdf\FpdfTpl, while TCPDF comes with own template methods.

        Usage

        The logic of FpdfTpl is very simple: You can start a template by calling beginTemplate(). All page content that is written with FPDF will be catched and encapsulated in the form XObject then.

        Because a form XObject can be reused on several pages and/or sizes it is not possible to use dynamic content like links in templates. If you call such a method a BadMethodCallException will be thrown.

        Following example will create a header template that will be re-used on every page then:

        PHP
        <?php
        use \setasign\Fpdi\FpdfTpl;
        
        require_once('fpdf/fpdf.php');
        require_once('fpdi2/src/autoload.php');
        
        $pdf = new FpdfTpl();
        $pdf->AddPage();
        
        $templateId = $pdf->beginTemplate();
        $pdf->setFont('Helvetica');
        $pdf->Text(10, 10, 'HEADING');
        $pdf->endTemplate();
        
        $pdf->useTemplate($templateId);
        
        for ($i = 9; $i > 0; $i--) {
            $pdf->AddPage();
            $pdf->useTemplate($templateId);
        }
        
        $pdf->Output();

        If a font or color was defined before starting the template, these states will be used in the template context.

        Anyhow, changes of the states which were done while writing to the template will be reset when ending a template.

        Methods

        beginTemplate()

        Begins a new template.

        endTemplate()

        Ends a template.

        getTemplateSize()

        Get the size of a template.

        useTemplate()

        Draws a template onto the page or another template.