PHP
  Home arrow PHP arrow Page 4 - Constructing Multi-Line PDF Documents ...
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

Constructing Multi-Line PDF Documents with PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 2
    2007-11-06

    Table of Contents:
  • Constructing Multi-Line PDF Documents with PHP 5
  • Review: building basic PDF documents using PHP 5
  • Displaying multiple lines of text on a single PDF document
  • Building a multi-line PDF document using the set_text_pos() method

  • 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

    Dell PowerEdge Servers

    Constructing Multi-Line PDF Documents with PHP 5 - Building a multi-line PDF document using the set_text_pos() method
    (Page 4 of 4 )

    As you learned in the previous section, including multiple lines of text in a given PDF file is actually a no-brainer process, since it only requires calling the handy “continue_text()” method as many times as necessary. However, it should be noticed that before displaying any strings on the file in question I utilized the “set_text_pos()” method to specify what X,Y coordinates should be used to start showing the pertinent text. This sounds pretty logical, right?

    You already understand how to put multiple lines of text in a specific PDF file. Therefore, the last example in this article will focus on demonstrating how to display several lines of text in a given PDF document, which will be positioned at different X,Y coordinates via the aforementioned “set_text_pos()” method.

    This being said, here’s the corresponding code sample, so have a look at it, please:

    // example creating a basic PDF document with PHP and multiple
    lines using the 'set_text_pos()' method


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

      $pdf->set_text_pos(50,450);

      $pdf->show('This is another line of text');

      $pdf->set_text_pos(50,400);

      $pdf->show('This is another line of text');

      $pdf->set_text_pos(50,350);

      $pdf->show('This is another line of text');

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

    }

    Certainly, after analyzing the signature of the above example, you’ll have to agree with me that positioning different chunks of text in a concrete PDF file using a set of predefined X,Y coordinates is a very understandable process, particularly when the respective “set_text_pos()” method is utilized.

    In addition to providing you with the previous code sample, I captured a screen shot below to show you the corresponding output generated by the example. Here it is:

    Well, at this point I assume that you are much better prepared to build a few basic multi-line PDF documents with PHP 5, particularly if you’ve studied in detail all of the code samples shown previously. As usual with many other topics on PHP development, practice is the best way to learn how to build dynamic PDF files, so I recommend that you create your own testing examples.

    Final thoughts

    In this second part of the series, you hopefully learned the basics for building basic PDF files that include multiple lines of text, via the proper combination of the useful “set_text_pos()”, “show()” and continue_text()” methods respectively.

    In the next tutorial, things will get even more interesting, since you’ll learn how to include some basic images into a given PDF document, in addition to building text flows.

    Now that you’ve been warned about the bunch of topics that will be covered in the upcoming tutorial, you won’t want to miss it!


    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 second installment of the series, you’ll learn the basics on...
     

       

    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 4 hosted by Hostway