PHP
  Home arrow PHP arrow Page 4 - 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 - Creating a basic A5 PDF document using PHP 5
    ( Page 4 of 4 )

    As I said in the section that you just read, the last code sample that I’m going to provide you in this tutorial is aimed at illustrating how to build a primitive PDF file with PHP 5, which will be rendered in A5 format. As you might guess, the document can be easily constructed by utilizing the same set of methods that you saw in the previous example, but in this case, the “begin_page_ext()” method indicates that the document to be created must fit the A5 format.

    Having explained that, here’s the respective code sample:

    // example creating a basic A5 PDF document with PHP 5

      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!");

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

    }

    I’m not going to spend much time explaining how the above example works, since it looks very similar to the ones that you learned in earlier sections of this article. The only detail to stress here is with reference to the use of the ““begin_page_ext()” method, which instructs the script to build a page whose dimensions must fit the A5 format.

    Below I included another screen shot that shows very clearly how the previous PDF file is outputted to the computer system:

    That’s all for the moment. Hopefully, all of the code samples shown in this tutorial will serve to get you started building some basic PDF files in PHP 5 with minor hassles. Happy coding!

    Final thoughts

    In this first tutorial of the series I walked you through using some basic methods that come bundled with the “PDFlib” library to build a few basic PDF documents using PHP 5. As you learned from all the code samples shown here, the process is quite simple to grasp. Many of these methods are very intuitive, particularly when it comes to creating PDF files that contain only basic text.

    In the next part of the series, I’m going to teach you how to build PDF files that include multiple lines of text, directly from your PHP 5 scripts, among other useful things. Now that you’ve been warned, you won’t want to miss it!



     
     
    >>> 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