resource pdf_new ( void )
bool pdf_open_file ( resource pdfdoc [, resource pdfdoc])
int pdf_findfont ( resource pdfdoc, string fontname, string encoding [, int embed])
bool pdf_begin_page ( resource pdfdoc, float width, float height)
bool pdf_setfont ( resource pdfdoc, string font, float size)
bool pdf_show_xy ( resource pdfdoc, string text, float x, float y)
bool pdf_end_page ( resource pdfdoc)
bool pdf_close ( resource pdfdoc)
bool pdf_delete ( resource pdfdoc)
Creating a PDF document is very much like creating a Flash movie or a picture in that to get the desired end result you state the list of drawing actions required to get there - drawing lines, text, adding fonts, etc. As in the prior two articles, you need to track the PDF document you are working with at all times as each function uses it. Let's get started in the usual manner - with a big chunk of PHP!
As you might have guessed by the long chapter title, creating even a simple PDF takes quite a few functions, so I suggest you make yourself a fresh cup of coffee before continuing.
<?php $pdf = pdf_new(); pdf_open_file($pdf, "/path/to/your.pdf"); $font = pdf_findfont($pdf, "Times-Roman", "host"); pdf_begin_page($pdf, 595, 842); pdf_setfont($pdf, $font, 30); pdf_show_xy($pdf, "Printing text is easy", 50, 750); pdf_end_page($pdf); pdf_close($pdf); pdf_delete($pdf); ?>
Starting at line one, we use pdf_new() to create a new PDF document and store it in $pdf. This value will be used in all the subsequent functions, so it is important to keep.
pdf_open_file() is used to open a file for writing. Note that the free version of PDFlib does not allow alteration of existing PDFs; this function merely creates a new PDF of the given filename. Naturally it will need to be somewhere your web server is able to write to, otherwise you will receive an error along the lines of "Fatal error: PDFlib error: function 'PDF_set_info' must not be called in 'object' scope in script.php on line XYZ"
The next line uses pdf_findfont() to find and load a font for use inside the generated PDF file. In the example, pdf_findfont() takes three parameters - the PDF document to work with, the name of the font to use, and which encoding to use. In the example above, $pdf is specified as the first parameter (as always). "Times-Roman" is specified as the font to use, which is one of the fourteen standard internal PDFlib fonts. The next parameter can be set to either "winansi" (Windows), "macroman" (Macintosh), "ebcdic" (EBCDIC code page 1047 machines), "builtin" (for symbol fonts), or "host" (winansi for Windows, macroman for Macintosh, etc). Generally it is smart to use "host", and leave PDFlib to do the hard work, so this is what is used in the code above.
pdf_findfont(), when successful, returns a font resource which is stored in $font. You may wish to add error-checking in your own scripts to add extra reliability.
At this point, we're ready to start on the main part of PDF generation. The first three lines merely set things up for the document - the next four, lines four to seven, are the page itself.
Reading the source, it is fairly easy to see that line four and line seven encapsulate one page in the generated PDF file. Objects and text outputted between a pdf_begin_page() and pdf_end_page() will affect that page, and multiple begin/end blocks are used to created multiple pages.
Note that pdf_begin_page() takes a second and third parameter which specify the X and Y point size of this page. The PDF format allows you to make your pages different point sizes from page to page - commonly used sizes are listed later on.
pdf_setfont() takes three parameters - the first is as per usual, the second parameter is the return value from pdf_findfont for the font you wish to use, and the final parameter is the size to use, in points. Immediately afterwards, we call pdf_show_xy() to place text into our page. Parameter two of pdf_show_xy() is the string to use, and parameters three and four are the X and Y co-ordinates at which to print the text.
Author's Note: Confusingly, there is a pdf_set_font() function that is deprecated - try not to get mixed up!
It is important to note that the Y parameter, the last parameter passed to pdf_show_xy(), is distance the text should appear above the page baseline, in points. That is, setting this parameter to 0 will have the bottom of a lowercase "a" at the very bottom of the page, and the bottom of a lowercase "y" outside the margins of the page.
With pdf_end_page called, the first and only page is completed, and all that is left to do is clean things up.
Cleaning up is done through the help of two functions - pdf_close() and pdf_delete(). They may sound somewhat similar, but you do need to call them both - pdf_close() cleans up the PDFlib memory and document-related resources, whereas pdf_delete() cleans up PHP's reference to $pdf and any other internal resources. Be sure to call them in the order shown above.
Save the code as pdf1.php and, after placing inside your public HTML directory, run it using your web browser. Note that there is no "Success!" message being printed out, however, you should find your PDF file has been created and is viewable in your PDF reader of choice.
Want to learn PHP 7?
Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!
If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!
Next chapter: Adding more pages and more style >>
Previous chapter: There's more than one way to do it
Home: Table of Contents
Copyright ©2015 Paul Hudson. Follow me: @twostraws.