PHP
  Home arrow PHP arrow Page 3 - PDF Generation With PHP
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

PDF Generation With PHP
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 87
    2002-08-14


    Table of Contents:
  • PDF Generation With PHP
  • Getting Started
  • Anatomy Lesson
  • Pretty As A Picture
  • The Shortest Distance Between Two Points
  • Square Peg, Round Hole
  • Heaven Is A Place On Earth
  • Piece Of Pie

  • 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


    PDF Generation With PHP - Anatomy Lesson
    ( Page 3 of 8 )

    Let's take a closer look at the code used in the example above.

    Creating a PDF file in PHP involves four basic steps: creating a handle for the document; registering fonts and colours for the document; writing or drawing to the handle with various pre-defined functions; and saving the final document.

    Let's take the first step - creating a handle for the PDF document.
    // create handle for new PDF document
    $pdf = pdf_new();
    This is accomplished via the pdf_new() function, which returns a handle to the document. This handle is then used in all subsequent operations involving the PDF document.

    Next up, you need to give the PDF file a name - this is accomplished via the pdf_open_file() function, which requires the handle returned in the previous operation, together with a user-defined file name.
    // open a file
    pdf_open_file($pdf, "philosophy.pdf");
    Once a document has been created, you can insert new pages in it with the pdf_begin_page() function,
    // start a new page (A4)
    pdf_begin_page($pdf, 595, 842);
    and end pages with the - you guessed it! - pdf_end_page() function.
    // end page
    pdf_end_page($pdf);
    Note that the pdf_begin_page() function requires two additional parameters, which represent the width and height of the page to be created in points (a point is 1/72 of an inch). In case math isn't your strong suit, the PHP manual provides width and height measurements for most standard page sizes, including A4, the one used in the example above.

    In between the calls to pdf_begin_page() and pdf_end_page() comes the code that actually writes something to the PDF document, be it text, images or geometric shapes. In this case, all I'm doing is writing a line of text to the document - so all I need to do is pick a font, and then use that font to write the text string I need to the document.

    Selecting and registering a font is accomplished via the pdf_findfont() and pdf_setfont() functions. The pdf_findfont() function prepares a font for use within the document, and requires the name of the font, the encoding to be used, and a Boolean value indicating whether or not the font should be embedded in the PDF file; it returns a font object, which may be used via a call to pdf_setfont().
    $arial = pdf_findfont($pdf, "Arial", "host", 1); pdf_setfont($pdf,$arial, 10);
    Once the font has been set, the pdf_show_xy() function can be used to write a text string to a particular position on the page.
    // print text
    pdf_show_xy($pdf, "There are more things in heaven and earth, Horatio,",50, 750);
     pdf_show_xy($pdf, "than are dreamt of in your philosophy", 50,730);
    As you can see, this function requires a handle to the PDF document, a reference to the font object to be used, the text string to be written (obviously!), and the X and Y coordinates of the position at which to begin writing the text. These coordinates are specified with respect to the origin (0,0), which is located at the bottom left corner of the document.

    Once the text has been written, the page is closed via a call to pdf_end_page(). You can then add one or more extra pages, or - as I've done here - simply close the document via pdf_close(). This will save the document contents to the file specified in the initial call to pdf_open_file(), and destroy the document handle created.

     
     
    >>> More PHP Articles          >>> More By icarus, (c) Melonfire
     

       

    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 2 Hosted by Hostway
    Stay green...Green IT