Sunday, June 17, 2007

Generate PDFs with PHP

Pretty As A Picture

Now, that was a very simple example - but PHP's PDF extension allows you to do a lot more than just write text to a page. Since a picture is worth a thousand words, consider this next example, which demonstrates the process of adding an image to your newly-minted PDF document.

// create handle for new PDF document
$pdf = pdf_new();

// open a file
pdf_open_file($pdf, "philosophy.pdf");

// start a new page (A4)
pdf_begin_page($pdf, 595, 842);

// get and use a font object
$arial = pdf_findfont($pdf, "Arial", "host", 1); pdf_setfont($pdf, $arial, 10);

// print text
pdf_show_xy($pdf, "There are more things in heaven and earth, Horatio,", 50, 750); pdf_show_xy($pdf, "than are dreamt of in your philosophy", 50, 730);

// add an image under the text
$image = pdf_open_image_file($pdf, "jpeg", "shakespeare.jpg"); pdf_place_image($pdf, $image, 50, 650, 0.25);

// end page
pdf_end_page($pdf);

// close and save file
pdf_close($pdf);
?>

Here's the PDF output:

1225_2 (click to view image)

Most of the magic here happens via the pdf_open_image_file() and
pdf_place_image() functions. The first one accepts an image type - GIF, JPEG, TIFF or PNG - and file name as arguments, and returns an image handle, which may then be re-used multiple times in the document.

The image handle returned in the previous step can be used by the pdf_place_image() function, which actually takes care of positioning the image at a particular point on the page. The coordinates provided to this function (the second and third arguments) refer to the position of the lower left corner of the image, while the fourth argument specifies the scaling factor to use when displaying the image (a scaling factor of 1 will show the image at actual size, while a factor of 0.5 will reduce the image to half its size).

The Shortest Distance Between Two Points

Why stop there? PHP's PDF extension comes with a whole bag of functions designed to let you draw lines, circles and other shapes in your PDF document. Consider the following example, which demonstrates the process of drawing a line.

// create handle for new PDF document
$pdf = pdf_new();

// open a file
pdf_open_file($pdf, "letterhead.pdf");

// start a new page (A4)
pdf_begin_page($pdf, 595, 842);

// get and use a font object
$arial = pdf_findfont($pdf, "Arial", "host", 1); pdf_setfont($pdf, $arial, 12);

// set a colour for the line
pdf_setcolor($pdf, "stroke", "rgb", 0, 0, 0);

// place a logo in the top left corner
$image = pdf_open_image_file($pdf, "jpeg", "logo.jpg"); pdf_place_image($pdf, $image, 50, 785, 0.5);

// draw a line under the logo
pdf_moveto($pdf, 20, 780);
pdf_lineto($pdf, 575, 780);
pdf_stroke($pdf);

// draw another line near the bottom of the page pdf_moveto($pdf, 20, 50); pdf_lineto($pdf, 575, 50); pdf_stroke($pdf);

// and write some text under it
pdf_show_xy($pdf, "Confidential and proprietary", 200, 35);

// end page
pdf_end_page($pdf);

// close and save file
pdf_close($pdf);
?>

Here's what you should see in the PDF document:

1225_3 (click to view image)

In this case, the process of drawing a line involves creative use of the pdf_moveto(), pdf_lineto() and pdf_stroke() functions.

In the example above, I'd like to draw a line from the position (20,780) to the new position (575, 780). In order to do this, I first need to place the cursor at the starting point (20,780), via a call to pdf_moveto().

pdf_moveto($pdf, 20, 780);

Next, I need to set the end point of the line, via pdf_lineto():

pdf_lineto($pdf, 575, 780);

Finally, the line is actually rendered using pdf_stroke().

pdf_stroke($pdf);

The stroke colour is set via a call to pdf_setcolor(), which accepts a number of parameters: the PDF document handle, whether the colour being set is for "stroke", "fill" or "both", the colour scheme to use (RGB or CMYK), and a list of colour values appropriate to the selected colour scheme.

pdf_setcolor($pdf, "stroke", "rgb", 0, 0, 0);

It's important to note that the list of colour values provided to
pdf_setcolor() must be specified in terms of percentage intensity - that is, the intensity of that colour, expressed as a percentage of the maximum intensity possible. So, if I wanted to set red (RGB: 255,0,0) as the stroke colour, my call to pdf_setcolor() would look like this:

pdf_setcolor($pdf, "stroke", "rgb", 1, 0, 0);

while a fill colour of yellow (RGB: 255,255,0) would be:

pdf_setcolor($pdf, "fill", "rgb", 1, 1, 0);

No comments: