PHP
  Home arrow PHP arrow Page 3 - Working with Images and Text Flows in PDF Files with PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 2
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    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
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT