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

Creating a basic A5 PDF document using PHP 5 - 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 said in the section that you just read, the last code sample that I’m going to provide you in this tutorial is aimed at illustrating how to build a primitive PDF file with PHP 5, which will be rendered in A5 format. As you might guess, the document can be easily constructed by utilizing the same set of methods that you saw in the previous example, but in this case, the “begin_page_ext()” method indicates that the document to be created must fit the A5 format.

Having explained that, here’s the respective code sample:

// example creating a basic A5 PDF document with PHP 5

  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 PDFs!");

// 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();



I’m not going to spend much time explaining how the above example works, since it looks very similar to the ones that you learned in earlier sections of this article. The only detail to stress here is with reference to the use of the ““begin_page_ext()” method, which instructs the script to build a page whose dimensions must fit the A5 format.

Below I included another screen shot that shows very clearly how the previous PDF file is outputted to the computer system:

That’s all for the moment. Hopefully, all of the code samples shown in this tutorial will serve to get you started building some basic PDF files in PHP 5 with minor hassles. Happy coding!

Final thoughts

In this first tutorial of the series I walked you through using some basic methods that come bundled with the “PDFlib” library to build a few basic PDF documents using PHP 5. As you learned from all the code samples shown here, the process is quite simple to grasp. Many of these methods are very intuitive, particularly when it comes to creating PDF files that contain only basic text.

In the next part of the series, I’m going to teach you how to build PDF files that include multiple lines of text, directly from your PHP 5 scripts, among other useful things. Now that you’ve been warned, you won’t want to miss it!

>>> 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: