The irony is, of course, PDFlib is now commercial and offers a PDFlib Lite. Either way, the PDFlib library has an unwieldy learning curve, and instead, I recommend FPDF for your general PDF usages. This is a freeware PDF library written completely in PHP, so no PECL or PEAR installation is required, which does slow it down. So, a large business shouldn’t use this to process hundreds of PDFs, but for smaller jobs it works.
Making a basic PDF document on the fly
I’ll show you how to make a basic PDF document with text blocks of different alignments, as well as double-spacing, headers and footers and displaying an image.
Let’s jump immediately into an example showing all of this:
I create a PDF in Portrait mode with a Letter size and measured in inches. Then, I set the margins to 1 inch, add our first page, and then set the Font to Times, Size 12. Then, my 3 dummy texts, and after that I set the fill color that will be the space behind my title’s text with a bold/underlined modifier. After that, I print the title, then reset the font and specify some different types of MultiCells. The page is made up of cells on an x,y plane in FPDF, which makes it pretty convenient to work with for basic PDF editing.
The headers and footers are functions automatically called in the parent class, but they do nothing by default. That means we have to redefine them to our needs. In this case, a name and page number as the header and a logo with a link as the footer. These are repeated on each page.
Note that in order to place the text in the proper location, I had to use the SetX and SetY functions. Don’t forget to reset them to their normal flow when you’re done in the header or footer, though!
Cells are individual units on the page. It is convenient to define a line with a single cell, but at the same time this can be achieved much more quickly by use of the MultiCell function which will automatically create a new cell as text is passed through. Write is just streamed text.
One thing to absolutely keep in mind is that you can’t output any text to the browser when trying to send this PDF to the browser, if you’re making it on the fly. If you’re making it for storage or later use, then there’s no problem outputting text to the user as well. This is accomplished by setting the parameters to Output appropriately.
The spacing is done with the line height. You have to do some math to figure out what you want. The line height is the entire height of the line, which means the lines are up against each other. That means whatever you set for the line height is spacing between the letters, as well!
With these skills in mind, you should be able to do whatever you want to a PDF. For even more functionality, there is a Scripts section, but of course there is always the PDFlib library which provides all kinds of specialization. FPDF is a highly documented library though, and it’s really simple to use. Good luck!