Specifications
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 mem-
ory 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’ certificates, 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_open() without parameters as follows:
$pdf = pdf_open();
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,
$width and $height and a few new ones: $inset, $border, and $inner. We will use $inset to
specify how many points wide the border at the edge of the page is,
$border to specify the
thickness of the main border, and $inner to 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 gd library is quite similar to drawing them with PDFlib.
The thin borders are easy. To create a rectangle, we use pdf_rect(), which requires as para-
meters 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 calcu-
late 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.
Generating Personalized Documents in Portable Document Format (PDF)
C
HAPTER 30
30
GENERATING
PERSONALIZED
DOCUMENTS IN
PDF
775
36 7842 CH30 3/6/01 3:40 PM Page 775