PHP
  Home arrow PHP arrow Page 2 - 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 
 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

    Dell PowerEdge Servers

    Working with Images and Text Flows in PDF Files with PHP 5 - Listing the source code of some previous examples
    (Page 2 of 4 )

    Before I proceed to show you how to include some basic images into a given PDF document, first I'd like you to recall properly how to use the handy "set_text_pos()" and "continue_text()" methods that I explained in the previous article of the series, in order to build PDF files that contain multiple lines of text.

    Regarding the utilization of these methods, below I listed a pair of illustrative examples that show how to implement them. Here are the corresponding code samples, so have a look at them, please:

    // example creating a basic PDF document with PHP and multiple
    lines using the 'continue_text()' 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->continue_text('This is another line of text');

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

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

    }

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

    }

    As you can see, the two examples above demonstrate how to utilize the "set_text_pos()", "show()", and "continue_text()" methods that come integrated with the PDFlib library to build a couple of PDF documents that include some basic multi-line texts.

    Of course, in this case, the "set_text_pos()" method makes it really easy to display a given string in a PDF file using a pair of X, Y coordinates, which can be expanded to scope two or more lines of text.

    All right, at this stage you've hopefully recalled the complete sequence of steps required to build basic PDF files that contain simple strings. Considering this, I believe that it's time to continue learning more methods that come bundled with the PDFlib package.

    In the upcoming section of this tutorial I'm going to show you how to incorporate a simple image into an existing PDF file to make it slightly more appealing. Of course, to see how this will be done, you have to click on the link below and keep reading.

    More PHP Articles
    More By Alejandro Gervasio


       · 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




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