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  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
Moblin 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM developerWorks
 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

Building PDF Documents with PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 9
    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:
      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

    Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!

    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


       · In this first part of the series, you’ll learn how to build basic PDF files from...
     

       

    PHP ARTICLES

    - Handling Attachments in MIME Email with PHP
    - Completing the Project Management Application
    - Sending MIME Email with PHP
    - Handling Files for a Project Management Appl...
    - Viewing and Editing Tasks for a Project Mana...
    - More on Private Methods with PHP 5 Member Vi...
    - Adding Tasks to a Project Management Applica...
    - Utilizing Private Methods with PHP 5 and Mem...
    - Making Changes in a Project Management Appli...
    - Defining Public and Protected Methods with M...
    - HTML for a Project Management Application
    - Using Subclasses and Accessors with Member V...
    - Implementing Internet Protocols with PHP
    - Project Management: The Application
    - Working with Private Properties to Protect P...




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