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  
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 
Moblin 
JMSL Numerical Library 
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: 4 stars4 stars4 stars4 stars4 stars / 10
    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


    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


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

       

    PHP ARTICLES

    - Validating Web Forms with the Code Igniter P...
    - Output Buffering
    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview





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