note 51315 added to function.pdf-begin-template
| From: | rz at daimi dot au dot dk | Date: | Sun, 27 Mar 2005 04:23:30 +0000 |
| Subject: | note 51315 added to function.pdf-begin-template | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-87041@lists.php.net to get a copy of this message | ||
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);
?>
----
Manual Page -- http://www.php.net/manual/en/function.pdf-begin-template.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+51315
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+51315&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+51315&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+51315&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+51315&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+51315&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+51315&report=yes
Search -- http://master.php.net/manage/user-notes.php