Home arrow PHP arrow Page 2 - Building PDF Documents with PHP 5

Building basic PDF documents with PHP 5 using an object-oriented approach - PHP

PHP is a versatile language with some surprising capabilities. It can even create PDF documents on the fly, with the right third-party PDF library. If you can't imagine why you'd want to do that, think about the various formats used by applications when retrieving data from databases -- and keep reading.

  1. Building PDF Documents with PHP 5
  2. Building basic PDF documents with PHP 5 using an object-oriented approach
  3. Working with different page sizes
  4. Creating a basic A5 PDF document using PHP 5
By: Alejandro Gervasio
Rating: starstarstarstarstar / 15
October 30, 2007

print this article



As I stated in the beginning of this article, building basic PDF files with PHP 5 is a straightforward process that can be easily learned even if you’re a novice PHP programmer. In simple terms, and assuming the corresponding PDFlib library has already been installed on your web server, creating a simple PDF document is reduced to spawning an instance of a bundled class, called “PDFlib”, and using some of its most common methods. Period.

But let me dispose of the theory for an instant and show you this process from a practical point of view. Take a look at the following code sample. It first creates a primitive PDF file that contains a basic text, and then displays this content by using a default PDF application:

// example creating a basic PDF (A4) document with PHP


try {

// create new instance of the 'PDFlib' class

$pdf=new PDFlib();

// open new PDF file


throw new PDFlibException("Error creating PDF document. ".$pdf-



  $pdf->set_info("Author","Alejandro Gervasio");

$pdf->set_info("Title","Example on using PHP to create PDF






  $pdf->show("PHP is great for creating PDF documents!");

// end page



// end document


// get buffer contents


// get length of buffer


// display PDF document

  header("Content-type: application/pdf");

   header("Content-Length: $len");

   header("Content-Disposition: inline; filename=example.pdf");

 echo $buffer;


  catch (PDFlibException $e){

   echo 'Error Number:'.$e->get_errnum()."n";

   echo 'Error Message:'.$e->get_errmsg();



As you can see in the above example, I first created a new instance of the aforementioned “PDFlib” class, and then used its “begin_document()” method to start an empty PDF document, which for now resides on the web server's memory. Next, I specified some basic information, like the document’s title and author, and after that I utilized the “begin_page_ext()” method to specify the dimensions of the page that will be used (in this case, I’m using an A4 page).

Still with me? All right, the next thing that the previous example does is call in the proper sequence the “set_font()” and “set_text_pos()” methods to specify what font type should be used to include text into the page, and the X, Y coordinates where the text in question should be displayed.

Also, the previous script uses the “show()” method to display a basic string on the PDF page. Then it closes the page and the document respectively, via the respective “end_page_ext()” and “end_document()” methods.

Lastly, the script finishes its execution by echoing the buffer contents in PDF format via the corresponding HTTP headers. Quite simple, right?

At this point you hopefully have grasped the logic that stands behind building basic PDF files with PHP 5. In simple words, you’ll always have to create an instance of the “PDFlib” class that you learned before, then open a new document and a new page (this is only applicable when using certain methods), and finally include some type of content into it, such as texts, images, etc. At the end of this procedure, the respective page and documents must be closed, and the contents of the buffer have to be displayed via the appropriate HTTP headers.

To complement the previous explanation, below I included an illustrative screen shot that shows the output generated by the previous script. Here it is:

So far, so good, right? At this stage you learned the basics of creating some simple PDF files with PHP 5, so I think it's time to develop another practical example. This will help you acquire a more solid background in how to work with PDF documents from your PHP 5 scripts.

In the next section I’ll show you how to use some of the methods that you saw earlier to create A3 documents in PDF format. Want to see how this will be done? All right, click on the link below and keep reading.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: