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

    PCmover - $15 Off with Coupon Code CJPH7Q

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

    According to the concepts that I deployed in the previous section, the PDFlib library provides PHP developers with some intuitive methods for including different types of images into a specific PDF file.

    Initially, PHP 4 offered a decent variety of native methods aimed at working with images and PDF documents in conjunction. Unfortunately, many of them have been deprecated. If you're still using this version of PHP and your php.ini file has been set up for working with PDF files, you're completely free to use these methods in accordance with your personal needs.

    In this case, though, I'm going to use only two methods for including images into a PDF document, called "load_image()" and "fit_image()" respectively, since they're pretty intuitive and easy to learn. However, you should take into account that these might not work as expected, depending on the respective versions of PHP and the "PDFlib" package that you have installed on your system. So be aware of this issue when building your PDF-related scripts.

    Now that I have clarified that point, please look at the following example. It shows how to include a basic image into a specific PDF file. The pertinent code sample is as follows:

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

    }

    As shown in the prior example, including a simple image into a specific PDF file is reduced to loading the pertinent graphic via the brand new "load_image()" method, and then displaying it using another method, named "fit_image()". As you might have guessed, the first method comes in handy for loading images of different types (JPEG, GIF, PNG, etc.) and the second one simply shows it at a specified position within the document in question. Quite simple, isn't it?

    Besides, as I explained earlier, the PDFlib library comes equipped with some  additional methods that can be used to display images on a given PDF file, but in my opinion the ones shown here are the most intuitive. If you're interested in learning how to use these additional methods, please visit the PHP official web site and read the PDF-related section.

    So far, so good. At this time you've hopefully grasped the logic required to display some basic images on a concrete PDF document, which indeed is a no-brainer process that can be tackled with minimal hassles. So assuming that you already learned this topic, let's move forward and see how to use some other useful methods that come integrated with the PDFlib library to display blocks of texts, called "text flows."

    Sounds quite interesting, doesn't it? So go ahead and read the next section. I'll be there, waiting for you.

    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

     
    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