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

       

    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 4 Hosted by Hostway
    Stay green...Green IT