PHP
  Home arrow PHP arrow Page 2 - Building PDF Documents 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? 
Google.com  
PHP

Building PDF Documents with PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 12
    2007-10-30


    Table of Contents:
  • Building PDF Documents with PHP 5
  • Building basic PDF documents with PHP 5 using an object-oriented approach
  • Working with different page sizes
  • Creating a basic A5 PDF document using PHP 5

  • 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


    Building PDF Documents with PHP 5 - Building basic PDF documents with PHP 5 using an object-oriented approach
    ( Page 2 of 4 )

    As I stated in the beginning of this article, building basic PDF files with PHP 5 is a straightforward process that can be easily learned even if you’re a novice PHP programmer. In simple terms, and assuming the corresponding PDFlib library has already been installed on your web server, creating a simple PDF document is reduced to spawning an instance of a bundled class, called “PDFlib”, and using some of its most common methods. Period.

    But let me dispose of the theory for an instant and show you this process from a practical point of view. Take a look at the following code sample. It first creates a primitive PDF file that contains a basic text, and then displays this content by using a default PDF application:

    // example creating a basic PDF (A4) document with PHP

     

    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","pdf_example.php");

      $pdf->set_info("Author","Alejandro Gervasio");

    $pdf->set_info("Title","Example on using PHP to create PDF
    docs");


      $pdf->begin_page_ext(595,842,"");

     

      $font=$pdf->load_font("Helvetica-Bold","winansi","");


      $pdf->setfont($font,24.0);

      $pdf->set_text_pos(50,800);

      $pdf->show("PHP is great for creating PDF documents!");

    // 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 in the above example, I first created a new instance of the aforementioned “PDFlib” class, and then used its “begin_document()” method to start an empty PDF document, which for now resides on the web server's memory. Next, I specified some basic information, like the document’s title and author, and after that I utilized the “begin_page_ext()” method to specify the dimensions of the page that will be used (in this case, I’m using an A4 page).

    Still with me? All right, the next thing that the previous example does is call in the proper sequence the “set_font()” and “set_text_pos()” methods to specify what font type should be used to include text into the page, and the X, Y coordinates where the text in question should be displayed.

    Also, the previous script uses the “show()” method to display a basic string on the PDF page. Then it closes the page and the document respectively, via the respective “end_page_ext()” and “end_document()” methods.

    Lastly, the script finishes its execution by echoing the buffer contents in PDF format via the corresponding HTTP headers. Quite simple, right?

    At this point you hopefully have grasped the logic that stands behind building basic PDF files with PHP 5. In simple words, you’ll always have to create an instance of the “PDFlib” class that you learned before, then open a new document and a new page (this is only applicable when using certain methods), and finally include some type of content into it, such as texts, images, etc. At the end of this procedure, the respective page and documents must be closed, and the contents of the buffer have to be displayed via the appropriate HTTP headers.

    To complement the previous explanation, below I included an illustrative screen shot that shows the output generated by the previous script. Here it is:

    So far, so good, right? At this stage you learned the basics of creating some simple PDF files with PHP 5, so I think it's time to develop another practical example. This will help you acquire a more solid background in how to work with PDF documents from your PHP 5 scripts.

    In the next section I’ll show you how to use some of the methods that you saw earlier to create A3 documents in PDF format. Want to see how this will be done? All right, click on the link below and keep reading.



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek