PHP
  Home arrow PHP arrow Page 2 - 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? 
Google.com  
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 - Adding an image and a text flow to a PDF file
    ( Page 2 of 4 )

    Since my personal experience always dictates that it’s much better to refresh topics covered in a previous article before learning new ones, I’m going to list two hands-on examples that were developed in the preceding tutorial regarding the incorporation of a simple image and text flow to a given PDF file.

    Here are the corresponding code samples, so have a close look at them, please:

    // example creating a basic PDF document and include a sample
    image

      try {

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

    // load image

       $img=$pdf->load_image("jpeg","sample_image.jpg","");

    // display image on page

      $pdf->fit_image($img,390,575,"");

    // close image resource

      $pdf->close_image($img);

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

    }

    // example creating a basic PDF document and display a text flow

      try{

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

    $textflow=$pdf->create_textflow('This is a sample
    string','fontname=Tahoma fontsize=30 encoding=winansi');

    // display text flow

      $pdf->fit_textflow($textflow,50,450,400,220,'');

    // delete text flow

      $pdf->delete_textflow($textflow);

    // 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’ll probably recall, the first example demonstrates very clearly how to include a “sample_image.jpg” image into a concrete PDF file, while the second one shows how to create and display a basic text flow in the same file, using some of the most relevant methods that come bundled with the PDFLib library.

    Of course, in both cases the incorporation of additional content to a specific PDF file is always performed after it has been opened, via the respective “begin_document()” method that you saw before. Now, do the above hands-on examples ring any bells to you? I'm sure they do!

    Okay, assuming that including images and blocks of text into a sample PDF document is now a familiar process to you, it’s time to continue exploring many other methods packaged with the PDFLib library. In the next section I’m going to teach you how to draw a few simple rectangles on a given PDF file, so if you’re interested in finding out more on this process, please click on the link that appears below and keep on reading.



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek