PHP
  Home arrow PHP arrow Page 4 - Drawing Basic Rectangles in PDF Docume...
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM Rational Software Development Conference
 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: 5 stars5 stars5 stars5 stars5 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Drawing Basic Rectangles in PDF Documents with PHP 5 - Displaying multiple rectangles on the same PDF file
    (Page 4 of 4 )

    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:

    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(30,100,150,150);

    // create another rectangle

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

    // display both rectangles

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

    }

    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!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Over the course of this fourth part of the series, the functionality provided by the...
     

       

    PHP ARTICLES

    - Setting Up a Web-based Image Hosting Service
    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery
    - Using Timers to Benchmark PHP Applications
    - Benchmarking Applications with PHP
    - Setting Up a Web-Based File Manager: PHPfile...
    - Developing a Modular Class For a PHP File Up...
    - Setting Up a Web-Based File Manager: bfExplo...
    - Defining a Custom Function for File Uploader...
    - Parsing Child Nodes with the DOM XML extensi...
    - Creating an Error Handling Module for a PHP ...
    - Accessing Attributes and Cloning Nodes with ...
    - Retrieving Information on Selected Files wit...
    - Handling HTML Strings and Files with the DOM...
    - Building File Uploaders with PHP 5

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway