A template is an "image" that can be drawn like a normal image, but is composed of pdflib-commands, directly specified in the program.
Templates can be used if you have a graphical pattern that you want to repeat several times - i.e. a logo repeated on top of each page. As the pdf-commands that draws the template are only included once in the file even if the template is shown several times, you can save a lot of space.
This program draws a simple fractal without the use of templates:
<?php
function drawmenger(&$pdf,$x,$y,$w,$level) {
if($level===0) {
pdf_rect($pdf,$x,$y,$w,$w);
pdf_fill($pdf);
return;
}
$w /= 3;
for($i=0; $i<3; $i++)
for($j=0; $j<3; $j++)
if($i!=1 || $j!=1)
drawmenger($pdf,$x+$w*$j,$y+$w*$i,$w,$level-1);
}
$pdf = pdf_new();
pdf_open_file($pdf);
pdf_begin_page($pdf, 595, 421);
drawmenger($pdf,50,50,300,6);
header('Content-type: application/pdf');
header('Content-disposition: inline; filename=test.pdf');
header('Content-length: ' . strlen($data));
echo $data;
pdf_end_page($pdf);
pdf_close($pdf);
pdf_delete($pdf);
?>
The file created is almost 1Mb.
Using templates to store the intermediate results reduces the filesize to a mere 3kb.
<?php
function drawmenger(&$pdf,$x,$y,$w,$level) {
if($level===0) {
pdf_rect($pdf,$x,$y,$w,$w);
pdf_fill($pdf);
return;
}
$w /= 3;
for($i=0; $i<3; $i++)
for($j=0; $j<3; $j++)
if($i!=1 || $j!=1)
drawmenger($pdf,$x+$w*$j,$y+$w*$i,$w,$level-1);
}
$pdf = pdf_new();
pdf_open_file($pdf);
pdf_begin_page($pdf, 595, 421);
drawmenger($pdf,50,50,300,6);
header('Content-type: application/pdf');
header('Content-disposition: inline; filename=test.pdf');
header('Content-length: ' . strlen($data));
echo $data;
pdf_end_page($pdf);
pdf_close($pdf);
pdf_delete($pdf);
?>