©
本文档使用 PHP中文网手册 发布
(PHP 4 >= 4.0.5, PECL pdflib >= 1.0.0)
PDF_begin_template — Start template definition [deprecated]
$pdfdoc
, float $width
, float $height
)Starts a new template definition.
This function is deprecated since PDFlib version 7, use PDF_begin_template_ext() instead.
[#1] rz at daimi dot au dot dk [2005-03-27 05:55:34]
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);
?>
[#2] rz at daimi dot au dot dk [2005-03-26 20:23:28]
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);
?>