PDF (Portable Document Format) files can be created with PHP. The power of creating PDF's with PHP is that they can be created on the fly. I.e. direct from user-input and/or combination of information stored on a database. You'll see this with online agencies when booking airline tickets. They will email you a pdf document containing your itenary.
PHP can be installed with the default pdf extension that comes with PHP, and is easily done in the extensions section of the php.ini file. (The semi-colon at the start means that the statement has been commented out).
;extension=php_pdf.dll
Using the php extension requires that the php extension is loaded, or you have access to the php.ini file to load it. On a shared webhost environment this may prove difficult...
www.fpdf.org contains the source code pdf library that PHP developers can use. The beauty of this pdf library is that you don't need to modify the php.ini file or do a rebuild of php.
Another aspect is that if you're keen to understand the mechanics behind building a pdf document the source file is approximately 1600 lines long.
Let's look at a basic 'Hello World' example.
1 $pdf=new FPDF();
2 $pdf->AddPage();
3 $pdf->SetFont('Arial','B',16);
4 $pdf->Cell(40,10,'Hello World!');
5 $pdf->Output('myFile.pdf', 'F');
Just like other frameworks, there is a set sequence that must be followed when creating pdf files. First and foremost you must create a new pdf object. This is done on line 1.
Next is to add a new page. This is done on Line 2. Then you need to set the font style (Line 3). Line 4 is where we create a cell 40 wide and 10 high that contains the text 'Hello World!'. It will be displayed within that cell with the Arial font, bolded and size 16.
Finally you need to 'output' the pdf file. The 'Output' function does offer 4 output options, and in this example we've chosen 'F' for File. The file will be called myFile.pdf. myFile.pdf will be created in the same directory that the script creating the file was called from.
This is the bare bones that is required for creating a pdf file. Admittely our pdf file only displays the text 'Hello World', however it does demonstrate how to work with the fpdf library.
1 $pdf=new FPDF();
Although we haven't specified any parameters in the constructor (line 1), the fpdf constructor can take 3 optional parameters. They are orientation (eg Portrait or Landscape), unit (measurement i.e. mm, cm, in, pt) and format (eg A4, letter, A3 etc).
2 $pdf->AddPage();
The AddPage function also accepts optional parameters. Orientation. So its possible to create a multi page pdf document alternating the page orientation if required.
3 $pdf->SetFont('Arial','B',16);
In our Hello World example, we've used all the possible parameters for the SetFont function. Family, Style and Size (in points). Other functions complement the SetFont function such as SetTextColor and SetFontSize.
4 $pdf->Cell(40,10,'Hello World!');
The Cell function is the workhorse function of the fpdf library. It is where we output text to the pdf document. We've supplied 3 parameters, however this function can take upto 8 parameters.
4 $pdf->Cell(40,10,'Hello World!');
The Cell function also comes with associated functions such as SetFillColor.
5 $pdf->Output('myFile.pdf', 'F');
The last function we have used is the Output function. This is the last function that is ever called and is used to 'finalize' the pdf document. I.E. write everything to the document. It accepts 2 optional parameters, the document name and the document destination.
Please note that I've found problems creating dynamic pdf files and outputing them straight to the browser. I recommend to output the pdf to file.
We could rewrite line 4 (shown below) so that the pdf document is dynamic.
$pdf->Cell(40,10,'Hello World!');
An example of a how to create a dynamic pdf document is shown below.
$message = 'Hello World'; $pdf->Cell(40,10,$message);
The fpdf library is a powerful php library that allows the easy creation of pdf files. As you've seen with the example code, creating a pdf file is fairly straight forward.
We've only just touched on the very basics of creating a pdf document. There are many more functions in the fpdf library worth investigating, and certainly the tutorials that are available at fpdf.org should be the first place to visit.
Other functionality available include footers and Headers, multiple page, multiple column pdf documents and including image files are part of the pdf document.