Professional Documents
Culture Documents
Templates
Templates
Templates
Informations
Author: David
License: FPDF
Description
This script adds templates to FPDF for building documents containing a large number of similar pages.
Typically, when a PDF is built with many pages
sharing the same layout while only text changes,
it can prove useful to use a template which will be defined only once as a single object and will
be
included in each page requiring that layout.
If the page layout is rather sophisticated, including a set of instructions only once in the PDF file
instead of having those present in each and every
page will significantly reduce the overall file size.
In the example provided, that size is divided by two when compared to a PDF produced the
standard way.
Separating layout design from PHP programming may make life easier for teams in charge of authoring documents.
Different people may be
assigned to layout design on one hand and data management on the other hand.
The first team will only manipulate FPDF instructions without
PHP programming while the other team
will focus on data without bothering with color, fonts, positions, etc.
Finally, a powerful anchor feature has also been added which further enhances this {development / design}
separation. It ties data to anchor IDs
defined in templates which specify either a set of
text properties (color, position, font) or a set of column widths.
Template rules
Templates must contain a subset of the FPDF instructions available in the FPDF class being used. Namely,
this subset includes the following
instructions: Cell(), Image(), Line(), Ln(),
Rect(), SetDrawColor(), SetFillColor(), SetFont(), SetFontSize(),
SetLeftMargin(),
SetLineWidth(), SetMargins(), SetRightMargin(),
SetTextColor(), SetX(), SetXY(), SetY(), Text(), Write().
If the current PDF class integrates some extended instructions such as Rotate(), RoundedRect(),
Circle() or Ellipse(), they are also
available within templates.
It is not allowed to define links in templates.
A page may include zero, one or several templates. A template may be present in all or in only a subset of pages.
Every line starting with one of the characters [';', '/', '*'] is considered comment and ignored.
Anchor definition
To define a text anchor within a template, use SetTextProp() with the following arguments:
SetTextProp(string anchor_id, float x_pos, float y_pos, float width, float height, string font_family, string font style, float font_size)
SetColumns(string anchor_id, float col00, float col01, float col02, ..., float col18, float col19)
Caution: anchor IDs are cross-template which means that they should be unique for a given PDF. If two
templates used in the same PDF have anchors
with the same ID, the last one will take precedence. There is however a
clear distinction between text and column anchors, which means that a text
anchor and a column anchor can
share the same ID.
verbose: boolean indicating if each processed instruction should be logged to stdout. Defaults to false.
If successful, this function returns a positive integer usable at a later stage by IncludeTemplate():
IncludeTemplate(int handle)
The method to output a text according to a set of properties defined in a template is:
If successful, the method returns an associative array with the following keys:
px => X position (float)
www.fpdf.org/en/script/script102.php 1/3
02/03/22, 21:28 Templates
The method to retrieve an array of column widths associated with an anchor ID is:
Example
Here's an example which builds a PDF containing 21 pages with data loaded from a CSV file.
The script invokes two distinct templates and demonstrates the usage of both types of anchors
(text and column).
<?php
require_once ('templates_fpdf.php');
$file_tpl1 = "template1.tpl";
$file_tpl2 = "template2.tpl";
$file_csv = "data.csv";
$headers = explode (";", $lines [0]); // Headers for columns for the first page
$rows = array_slice ($lines, 1); // Data doesn't include the first row which contains headers
// Template #1 is used for the part which builds one page per employee
if ($template1 <= 0) {
// ====================================================
// ====================================================
$pdf->AddPage("L");
// Template #2 is used for the part which builds a table containing all employees
if ($template2 <= 0) {
$pdf->IncludeTemplate ($template2);
// In the table of the first page, take into account only a subset of fields of CSV file; say fields #0,#2,#3,#5,#6,#7
// Column interspace is 1
// Column interspace is 1
// Last fill boolean parameter switches from false to true to achieve a "zebra" effect
$pdf->Cell ($pcol [$ii], $ptxp ['iy'], $data, "", 0, "L", $jj & 1);
www.fpdf.org/en/script/script102.php 2/3
02/03/22, 21:28 Templates
// ==================================================
// ==================================================
$pdf->AddPage("P");
$pdf->IncludeTemplate ($template1);
$page_nb = $pdf->PageNo();
$nb_pages = $pdf->PageNo();
$file_pdf = "ex.pdf";
$pdf->Output("F", $file_pdf);
print (" >> File '$file_pdf' generated: " . "$nb_pages pages - " . filesize($file_pdf) . " bytes\n");
?>
Download
ZIP | TGZ
www.fpdf.org/en/script/script102.php 3/3