PHP
  Home arrow PHP arrow Page 4 - Working with Images and Text Flows in ...
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

Working with Images and Text Flows in PDF Files with PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2007-11-13

    Table of Contents:
  • Working with Images and Text Flows in PDF Files with PHP 5
  • Listing the source code of some previous examples
  • Displaying a basic image
  • Displaying a basic text flow

  • 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

    PCmover - $15 Off with Coupon Code CJPH7Q

    Working with Images and Text Flows in PDF Files with PHP 5 - Displaying a basic text flow
    (Page 4 of 4 )

    As I stated in the section that you just read, the PDFlib package provided PHP developers with a bunch of handy methods aimed at displaying in a specific PDF document, several blocks of text, called "text flows."

    Basically, these blocks can be easily placed at any position within the pertinent document. It's also possible to specify their respective width and height values, which can be useful if you want to include strings that have predefined dimensions.

    However, let me put aside this boring theory for a moment and show you a practical example of how to include a basic text flow into a primitive PDF file. The corresponding code sample is listed below, so have a close look at it:

    try{

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

    // 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 can see, the above example uses three new methods, called "create_text_flow()", "fit_text_flow()", and "delete_text_flow()" to build a simple block of text, and then display this block at a specified position within the PDF file, and finally delete it from the respective buffer.

    In addition, it's worth noting here how the "fit_text_flow()" method used in this case to specify the respective width and height values should be assigned to the text flow just created.

    And finally, to extend the explanation of including a basic text flow into a given PDF file, below I included a screen shot that shows the output generated by the previous script: 

    All right, having demonstrated how to include one basic text flow into a simple PDF file using the PDFlib package in conjunction with PHP 5, I hope that you'll have a strong enough foundation to start developing your own testing examples, with the purpose of acquiring a solid background in building PDF documents with PHP.

    Final thoughts

    That's all for the moment. In this third part of the series I showed you how to use a group of additional methods bundled with the "PDFlib" library, how to include images into a given PDF file, and introduced simple blocks of texts, called "text flows."

    In the upcoming article, I'm going to teach you how to display a few basic shapes within a PDF file, which can be useful if you want to build PDF documents that contain richer contents.

    To learn more about these interesting topics, don't miss the next part of the series!


    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.

       · If you read the two previous installments of this series, most likely you’ll want to...
       · I am using your articles to learn to use PDFlib but I am getting an error with the...
       · Thank you for posting your comments on my PHP article. Concerning your question,...
       · I have tried to load the image using the full url, as I posted before, using the...
       · Thanks again for the comments. Here’s an example that worked on my system, and...
     

       

    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