Chapter 10. PDF
Adobe's Portable Document Format (PDF) provides a popular way to get a consistent look, both on screen and when printed, for documents. This chapter shows how to dynamically create PDF files with text, graphics, bookmarks, and more.
Dynamic construction of PDF files opens the door to many applications. You can create almost any kind of business document, including form
10.1 PDF Extensions
PHP has several libraries for generating PDF documents. This chapter shows how to use the popular
Since pdflib is the most mature and has the most features, that is the library we cover in this chapter. The basic concepts of the structure and features of a PDF file are common to all the libraries, though.
10.2 Documents and Pages
A PHP document is made up of a number of pages. Each page contains text and/or images. This section shows you how to make a document, create pages in that document, put text onto the pages, and send the pages back to the browser when you're done.
10.2.1 A Simple Example
Let's start with a simple PDF document. Example 10-1 simply places "Hello world!" on a page and then displays the resulting PDF document.
Example 10-1. Hello world in PDF
<?php $pdf = pdf_new( ); pdf_open_file($pdf); pdf_set_info($pdf,'Creator','hello.php'); pdf_set_info($pdf,'Author','Rasmus Lerdorf'); pdf_set_info($pdf,'Title','Hello world (PHP)'); pdf_begin_page($pdf,612,792); $font = pdf_findfont($pdf,'Helvetica-Bold','host',0); pdf_setfont($pdf,$font,38.0); pdf_show_xy($pdf,'Hello world!',50,700); pdf_end_page($pdf); pdf_set_parameter($pdf, "openaction", "fitpage"); pdf_close($pdf); $buf = pdf_get_buffer($pdf); $len = strlen($buf); header('Content-Type: application/pdf'); header("Content-Length: $len"); header('Content-Disposition: inline; filename=hello.pdf'); echo $buf; pdf_delete($pdf); ?>
Figure 10-1. Hello world in a PDF document
10.2.2 Initializing the Document
In Example 10-1, we started by calling
, to create a new PDF data structure, followed by
pdf_open_file( pdf [, filename ]);
The output of
is sent to
10.2.3 Setting Metadata
pdf_set_info( pdf , fieldname , value );
There are five standard field
In addition to informational fields, the
$value = pdf_get_parameter( pdf , name ); pdf_set_parameter( pdf , name , value );
A useful parameter to set is openaction , which lets you specify the zoom (magnification) of the file when it's opened. The values "fitpage" , "fitwidth" , and "fitheight" fit the file to the complete page, the width of the page, and the height of the page, respectively. If you don't set openaction , your document is displayed at whatever zoom the viewer had set at the time the document was opened.
10.2.4 Creating a Page
A page starts with a call to pdf_begin_page( ) and ends with a call to pdf_end_page( ) :
pdf_end_page( pdf );
You specify the paper
Table 10-1. Paper sizes
Here is some typical begin/end page code:
<?php pdf_begin_page($pdf, 612, 792); // US-Letter // code to create actual page content would go here pdf_end_page($pdf); ?>
10.2.5 Outputting Basic Text
To put text on a page, you must select the font you want to use, set the default font to be that font at a particular size, and then add the text. For example:
$font = pdf_findfont($pdf, "Times-Roman", "host", 0); pdf_setfont($pdf, $font, 48); pdf_show_xy($pdf, "Hello, World", 200, 200);
With PDF documents, the (0,0) coordinate indicates the bottom-left corner of the page. In later sections we'll examine the different aspects of fonts and text layout and explain these functions in detail.
10.2.6 Terminating and Streaming a PDF Document
Call pdf_close( ) to complete the PDF document. If no filename was provided in the pdf_open_file( ) call, you can now use the pdf_get_buffer( ) function to fetch the PDF buffer from memory. To send the file to the browser, you must send Content-Type, Content-Disposition, and Content-Length HTTP headers, as shown in Example 10-1. Finally, call pdf_delete( ) to free the PDF file once it's sent to the browser.