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

Displaying multiple rectangles on the same PDF file - 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 I explained in the previous section, it’s perfectly feasible to display more than one rectangle in the same PDF file by using the “rect()” and “stroke()” methods that were reviewed earlier. To demonstrate this concept as clearly as possible, below I coded another hands-on example that utilizes the methods to include two different rectangles in the sample PDF document.

The respective 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


// create another rectangle


// display both rectangles


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



See how easy it is to display multiple rectangles in the same PDF file? I guess you do! In this specific situation, the “rect()” method is called twice to define the respective rectangles at different locations, and then they’re displayed by using the corresponding “stroke()” method.

You may want to have a look at the output produced by the above example. Below I included a screen shot that shows how the pertinent rectangles are displayed in the same PDF document:

Also, as usual with many of my articles on PHP development, you’re free to tweak the source code in all of the code samples shown here to acquire more practice in building PDF files with PHP 5. Happy coding!

Final thoughts

In this fourth installment of the series, you hopefully learned how to draw a few simple rectangles in a sample PDF file, which can be useful if you want to add some decorative effects to the file in question.

Nonetheless, this educational journey surrounding the creation of PDF documents with PHP 5 has not ended yet, since there are some other useful methods bundled with the handy PDFlib library that need to be properly reviewed.

Thus, in the last tutorial of the series, I’m going to show you how to display several blocks of text at different positions within a given PDF file, by using another useful method called “show_xy()”.

Now that you know what the last article will be about, are you going to miss it? I hope not!

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