PHP
  Home arrow PHP arrow Page 3 - Drawing Basic Rectangles in PDF Documents with PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Drawing Basic Rectangles in PDF Documents with PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2007-11-20


    Table of Contents:
  • Drawing Basic Rectangles in PDF Documents with PHP 5
  • Adding an image and a text flow to a PDF file
  • Drawing some basic rectangles
  • Displaying multiple rectangles on the same PDF file

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Drawing Basic Rectangles in PDF Documents with PHP 5 - Drawing some basic rectangles
    ( Page 3 of 4 )

    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
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT