PHP
  Home arrow PHP arrow Page 2 - Positioning Strings with the show_xy()...
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 
 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

Positioning Strings with the show_xy() Method in PDF Documents with PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2007-11-27

    Table of Contents:
  • Positioning Strings with the show_xy() Method in PDF Documents with PHP 5
  • Inserting Blocks of Text
  • Going One Step Further
  • Completion

  • 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

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

    Positioning Strings with the show_xy() Method in PDF Documents with PHP 5 - Inserting Blocks of Text
    (Page 2 of 4 )

    Inserting blocks of text into an existing PDF file: working with the versatile “show_xy()” method

    As you saw in the earlier articles of this series, several times I used the “show()” method to insert a string of text into a sample PDF file, which hopefully demonstrated how easy it is to work with. However, it should be noticed here that this method itself is rather useless, particularly in those cases where a determined string must be positioned using a pair of X,Y coordinates.

    Of course, you may say that this accurate positioning procedure can be achieved with the “set_text_pos()” method, which was also reviewed previously, but this implies having to work with two different methods. Fortunately, the “PDFlib” library comes equipped with a handy method, named “show_xy()”, which can be used to display a given string in any position of a PDF document, in this way replacing the two methods referenced above.

    Having explained in theory how the “show_xy()” method works, it’s time to analyze it from a practical point of view. So below I coded an illustrative example that shows its considerable functionality. Here it is:

      try{

     

    // example creating a basic PDF document and display a
    basic text using the 'show_xy()' method

    // 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->show_xy("PHP is great for creating PDFs!",30,500);

     

    // 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 demonstrates quite clearly how to use the “show_xy()” method to display a trivial string of text on a given PDF document. In this concrete situation, the method in question was instructed to show the respective text using an offset of 30 for the X coordinate and a value of 500 for the respective Y axis.

    If you analyze in detail the previous code sample, it’s quite evident how useful the “show_xy()” method can be, particularly when the PDF file being created includes only some strings of text and nothing else. Pretty good, right?

    In addition, with the purpose of completing even more of the previous explanation of this handy method, below I included a picture that shows the output generated by the prior hands-on example:

    So far, so good. At this stage you hopefully learned the basics of utilizing the helpful “show_xy()” method to display different blocks of text using a predefined pair of X,Y coordinates. However, if you’re anything like me, then you might want to see some additional examples regarding the implementation of this method in concrete cases.

    Thus, bearing this in mind, in the course of the upcoming section, I’m going to set up another practical example, aimed at providing you with more material on using the “show_xy()” method.

    To learn how this example will be developed, go ahead and read the next few lines. They’re just one click away.

    More PHP Articles
    More By Alejandro Gervasio


       · Over the course of this last installment of the series, you’ll learn how to use 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 1 hosted by Hostway