Home arrow PHP arrow Page 3 - Drawing Basic Rectangles in PDF Documents with PHP 5

Drawing some basic rectangles - PHP

If you’re a PHP developer who’s searching for a friendly guide on how to build dynamic PDF files within your PHP 5-based applications, then look no further, and start reading this series now!

  1. Drawing Basic Rectangles in PDF Documents with PHP 5
  2. Adding an image and a text flow to a PDF file
  3. Drawing some basic rectangles
  4. Displaying multiple rectangles on the same PDF file
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
November 20, 2007

print this article



As you might guess, drawing basic shapes within a concrete PDF document is a process very similar to the one I described regarding how to include text flows and images. In this case, I’m going to use two brand new methods packaged with the PDFLib library, called “rect()” and “stroke()” respectively. The first one, as its name suggests, is responsible for drawing a basic rectangle, and the second one is tasked with filling it with a predefined foreground color.

Having explained how these two methods work, it’s time for you to see a concrete application of them, so I suggest you to take a look at the following example, which shows how to draw a primitive rectangle in a sample PDF file.

Having said that, the corresponding code sample is as follows:


// example creating a basic PDF document and display a
basic rectangle

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

// create rectangle


// display rectangle


// 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, the above hands-on example is very simple to grasp> It uses the aforementioned “rect()” method, obviously to draw a basic rectangle, and then displays it on the pertinent PDF document by calling the “stroke()” method. Not rocket science, right?

In addition, if it's still not clear to you how the previous methods do their thing, you may want to look at the following image, which shows the output generated by the above script. The image in question is as follows:

Here you have it. Now you are hopefully learning the basic steps required to draw a simple rectangle on a given PDF document, which can be quite useful in those cases where you need to include this type of shape to make it more attractive. Anyway, as you saw earlier, including a few basic rectangles in a PDF file is in fact a straightforward task that can be performed with minor efforts.

So far, so good. At this point, I showed you how to use the “PDFLib” library to draw a primitive rectangle on a sample PDF file. Nonetheless, I’d like to finish this tutorial by coding for you another example. It will demonstrate how to draw multiple rectangles on the same file.

As you might have guessed, this example will be developed in detail in the last section of this article, so click on the link below and read the next few lines.

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