pdf_begin_template

(PHP 4 >= 4.0.5, PECL)

pdf_begin_template -- Starts new template

Description

int pdf_begin_template ( resource pdfdoc, float width, float height )

Start a new template definition.


add a note add a note User Contributed Notes
rz at daimi dot au dot dk
27-Mar-2005 09:55
Sorry. In the previous note I somehow managed to include the same program twice.
The program that uses templates to draw the fractal is

<?php
function initmengers(&$pdf,$level) {
  static
$mengers;
  if(
$level>0) initmengers($pdf,$level-1);
 
$mengers[$level] = pdf_begin_template($pdf,1,1);
  if(
$level===0) {
  
pdf_rect($pdf,$x,$y,1,1);
  
pdf_fill($pdf);
  }
  else {
   for(
$i=0; $i<3; $i++)
     for(
$j=0; $j<3; $j++)
   if(
$i!=1 || $j!=1)
    
pdf_place_image($pdf,$mengers[$level-1],$j/3,$i/3,1/3);
  }
 
pdf_end_template($pdf);
  return
$mengers[$level];
}
$pdf = pdf_new();
pdf_open_file($pdf);
$pic = initmengers($pdf,6);
pdf_begin_page($pdf, 595, 421);
pdf_place_image($pdf,$pic,50,50,300);
pdf_end_page($pdf);
pdf_close($pdf);
$data = pdf_get_buffer($pdf);
header('Content-type: application/pdf');
header('Content-disposition: inline; filename=test.pdf');
header('Content-length: ' . strlen($data));
echo
$data;
pdf_delete($pdf);
?>
rz at daimi dot au dot dk
27-Mar-2005 12:23
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);
?>