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!

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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:

  try{

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

   if(!$pdf->begin_document("","")){

throw new PDFlibException("Error creating PDF document. ".$pdf-
>get_errmsg());

}

  $pdf->set_info("Creator","example.php");

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

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

  $pdf->begin_page_ext(421,595,"");

 

   $font=$pdf->load_font("Helvetica-Bold","winansi","");

  $pdf->setfont($font,24.0);

  $pdf->set_text_pos(50,500);

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

// create rectangle

  $pdf->rect(100,100,150,150);

// display rectangle

  $pdf->stroke();

// end page

  $pdf->end_page_ext("");

// end document

  $pdf->end_document("");

// get buffer contents

   $buffer=$pdf->get_buffer();

// get length of buffer

   $len=strlen($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();

exit();

}

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
   

PHP ARTICLES

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