As you can see, it is quite similar to the others, except that the border is simpler and the star looks a little dif-ferent.This is because we have drawn them into the document rather th
Trang 1// outline ribbon 1 pdf_moveto($pdf, 630, 150);
pdf_lineto($pdf, 610, 55);
pdf_lineto($pdf, 632, 69);
pdf_lineto($pdf, 646, 49);
pdf_lineto($pdf, 666, 150);
pdf_closepath($pdf);
pdf_stroke($pdf);
// draw ribbon 2 pdf_moveto($pdf, 660, 150);
pdf_lineto($pdf, 680, 49);
pdf_lineto($pdf, 695, 69);
pdf_lineto($pdf, 716, 55);
pdf_lineto($pdf, 696, 150);
pdf_closepath($pdf);
pdf_fill($pdf);
// outline ribbon 2 pdf_moveto($pdf, 660, 150);
pdf_lineto($pdf, 680, 49);
pdf_lineto($pdf, 695, 69);
pdf_lineto($pdf, 716, 55);
pdf_lineto($pdf, 696, 150);
pdf_closepath($pdf);
pdf_stroke($pdf);
pdf_setrgbcolor_fill($pdf, 8, 0, 0); //red
//draw rosette draw_star(665, 175, 32, 57, 10, $pdf, true);
//outline rosette draw_star(665, 175, 32, 57, 10, $pdf, false);
// finish up the page and prepare to output pdf_end_page($pdf);
pdf_close($pdf);
$data = pdf_get_buffer($pdf);
// generate the headers to help a browser choose the correct application header('Content-type: application/pdf');
Listing 30.6 Continued
Trang 2header('Content-disposition: inline; filename=test.pdf');
header('Content-length: ' strlen($data));
// output PDF echo $data;
}
function draw_star($centerx, $centery, $points, $radius,
$point_size, $pdf, $filled) {
$inner_radius = $radius-$point_size;
for ($i = 0; $i<=$points*2; $i++ ) {
$angle= ($i*2*pi())/($points*2);
if($i%2) {
$x = $radius*cos($angle) + $centerx;
$y = $radius*sin($angle) + $centery;
} else {
$x = $inner_radius*cos($angle) + $centerx;
$y = $inner_radius*sin($angle) + $centery;
} if($i==0) pdf_moveto($pdf, $x, $y);
else if($i==$points*2) pdf_closepath($pdf);
else pdf_lineto($pdf, $x, $y);
} if($filled) pdf_fill_stroke($pdf);
else pdf_stroke($pdf);
}
?>
The certificate produced using this script is shown in Figure 30.7 As you can see, it is quite similar to the others, except that the border is simpler and the star looks a little dif-ferent.This is because we have drawn them into the document rather than using an existing clip art file
Listing 30.6 Continued
Trang 3Figure 30.7 pdflib.php draws the certificate into a PDF document.
We will look at some of the parts of this script that are different from the previous examples
Visitors need to get their own details on a certificate, so we will create the document
in memory rather than in a file If we wrote it to a file, we would need to worry about mechanisms to create unique filenames, stop people from snooping into others’ certifi-cates, and determine a way to delete older certificate files to free up hard drive space on our server In order to create a document in memory, we call pdf_new()without param-eters followed by a call to pdf_open_file()as follows:
$pdf = pdf_new();
pdf_open_file($pdf);
Our simplified border will consist of three stripes: a fat border and two thin borders, one inside the main border and one outside.We will draw all of these as rectangles
To position the borders in such a way that we can easily alter the page size or the appearance of the borders, we will base all the border positions on the variables that we already have,$widthand $heightand a few new ones:$inset,$border, and $inner
We will use $insetto specify how many points wide the border at the edge of the page
is,$borderto specify the thickness of the main border, and $innerto specify how wide the gap between the main border and the thin borders will be
If you have drawn with another graphics API, drawing with PDFlib will present few surprises If you haven’t read Chapter 19, “Generating Images,” you might find it helpful
to do so, as drawing images with the gdlibrary is quite similar to drawing them with PDFlib
Trang 4The thin borders are easy.To create a rectangle, we use pdf_rect(), which requires as parameters the PDF document identifier, the x and y coordinate of the rectangle’s lower left corner, and the width and height of the rectangle Because we want our layout to be flexible, we calculate these from the variables we have set
pdf_rect($pdf, $inset-$inner,
$inset-$inner,
$width-2*($inset-$inner),
$height-2*($inset-$inner));
The call to pdf_rect()sets up a path in the shape of a rectangle In order to draw that shape, we need to call the pdf_stroke()function as follows:
pdf_stroke($pdf);
In order to draw the main border, we need to specify the line width.The default line width is 1 point.The following call to pdf_setlinewidth()will set it to $border(in this case 10) points:
pdf_setlinewidth($pdf, $border);
With the width set, we again create a rectangle with pdf_rect()and call pdf_stroke()
to draw it
pdf_rect($pdf, $inset+$border/2,
$inset+$border/2,
$width-2*($inset+$border/2),
$height-2*($inset+$border/2));
pdf_stroke($pdf);
After we have drawn our one wide line, we need to remember to set the line width back to 1 with this code:
pdf_setlinewidth($pdf, 1.0);
We are going to usepdf_show_xy()to position each line of text on the certificate For most lines of text, we are using a configurable left margin ($startx) as the x coordinate and a value chosen by eye as the y coordinate As we want the heading centered on the page, we need to know its width in order to position the left hand side of it.We can get the width using pdf_stringwidth() The call
pdf_stringwidth($pdf, 'PHP Certification');
will return the width of the string "PHP Certification"in the current font and font size
As with the other versions of the certificate, we will include a signature as a scanned bitmap.The following three statements
$signature = pdf_open_image_file($pdf, 'png', 'signature.png');
pdf_place_image($pdf, $signature, 200, 75, 1);
pdf_close_image($pdf, $signature);
Trang 5will open a PNG file containing the signature, add the image to the page at the specified location, and close the PNG file Other file types can also be used.The only parameter that might not be self explanatory is the fifth parameter to pdf_place_image().This function is not limited to inserting the image at its original size.The fifth parameter is a scale factor.We have chosen to display the image at full size and used 1 as the scale fac-tor, but could have used a larger number to enlarge the image, or a fraction to shrink it The hardest item to add to our certificate using PDFlib is the rosette.We cannot automatically open and include a Windows Meta File containing the rosette we already have, but we are free to draw any shapes we like
In order to draw a filled shape such as one of the ribbons, we can write the following code
Here we set the stroke or line color to be black and the fill or interior color to be a navy blue:
pdf_setrgbcolor_fill($pdf, 0, 0, 4); //dark blue pdf_setrgbcolor_stroke($pdf, 0, 0, 0); // black
Here we set up a five-sided polygon to be one of our ribbons and then fill it:
pdf_moveto($pdf, 630, 150);
pdf_lineto($pdf, 610, 55);
pdf_lineto($pdf, 632, 69);
pdf_lineto($pdf, 646, 49);
pdf_lineto($pdf, 666, 150);
pdf_closepath($pdf);
pdf_fill($pdf);
As we would like the polygon outlined as well, we need to set up the same path a sec-ond time, but call pdf_stroke()instead of pdf_fill()
As the multipointed star is a complex repetitive shape, we have written a function to calculate the locations in the path for us Our function is called draw_star()and requires x and y coordinates for the center, the number of points required, the radius, the length of the points, a PDF document identifier, and a Boolean value to indicate if the star shape should be filled in or just an outline
The draw_star()function uses some basic trigonometry to calculate locations for a series of points to lay out a star For each point we requested our star to have, we find a point on the radius of the star and a point on a smaller circle $point_sizewithin the outer circle and draw a line between them One thing worth noting is that PHP’s trigonometric functions such as cos()andsin()work in radians rather than degrees Using a function and some mathematics, we can accurately generate a complex repet-itive shape Had we wanted a complicated pattern for our page border, we could have used a similar approach
When all our page elements are generated, we need to end the page and the docu-ment