PHP
  Home arrow PHP arrow Page 3 - 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? 
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 - Working with different page sizes
    ( Page 3 of 4 )

    In the previous section, you hopefully learned how to build an A4 PDF document by using some methods that come integrated with the already familiar “PDFlib” package. I’m going to develop yet another practical example, this time to create an A3 document, which also will contain some basic text.

    The corresponding code sample is as follows:

    // example creating a basic A3 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(842,1190,"");

     

       $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, building A3 PDF documents with PHP 5 doesn’t differ too much from creating one that has an A4 format. The above script, which is responsible for constructing the document, uses the same methods that you learned in the previous section, but in this case the “begin_page_ext()” method specifies that the document being created should fit the A3 format, that is 842px X 1190px respectively.

    In addition, to complete the above explanation, I included an image (actually, its real size has been changed for editing reasons) that shows the output generated by the previous script:

    All right, after studying the previous example, you’ll have to agree with me that building some basic PDF files with PHP 5 is indeed a no-brainer process that can be tackled with minor problems. As you saw, the procedure is reduced to spawning an instance of the corresponding “PDFlib” class, and then calling some of its numerous methods, depending on what kind of content you want to include into the file.

    Assuming that you grasped how the previous example works, in the last section of this tutorial I’m going to provide you with an additional code sample, which will show you how to create the same PDF document that you saw earlier, but this time in A5 format.

    To see how this last example will be developed, jump ahead and read the next few lines.



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