PHP
  Home arrow PHP arrow Page 2 - Generate PDF Documents with PHP on the Windows Platform
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

Generate PDF Documents with PHP on the Windows Platform
By: K.K.Sou
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2008-09-16


    Table of Contents:
  • Generate PDF Documents with PHP on the Windows Platform
  • Hello world PDF
  • Finding the filename of the truetype font
  • Generating PDF documents for browsers
  • Producing a simple bar graph in a PDF document

  • 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


    Generate PDF Documents with PHP on the Windows Platform - Hello world PDF
    ( Page 2 of 5 )

    As with any other programming language, the best way to start exploring the PHP PDF library is a hello world example.

    First, we create a new pdf document named "helloworld.pdf":


    $pdf = pdf_new();

    pdf_open_file($pdf, 'helloworld.pdf');


    Then we start a new page:


    pdf_begin_page($pdf, 612, 792); // Letter size


    In case you are wondering why we're using the strange numbers of 612 and 792, these are the dimensions for a letter size paper. A letter size paper is 8.5” by 11”. The 612 is obtained by 8.5 * 72. Similarly, 11 * 72 = 792. Hence the two numbers.

    If you need to produce an A4-size pdf document, use:


    pdf_begin_page($pdf, 595, 842); // A4 size


    An A4-size paper is 210mm x 297mm, which is 8.27” by 11.69”. Multiply these by 72, and you get 595 by 842.

    After we have created a new page, before we can start printing any text on the new page, we need to set up the font first.

    pdf_set_parameter($pdf, 'FontOutline',

    'Arial=c:windowsfontsarial.ttf');

    $font = pdf_findfont($pdf, "Arial", "host", 1);

    pdf_setfont($pdf, $font, 48);


    We are now ready to print “hello world” on the page:


    pdf_show_xy($pdf, "hello world, PDF!", 50, 680);


    And we wrap up the pdf document with:


    pdf_end_page($pdf);

    pdf_close($pdf);

    pdf_delete($pdf);


    Below is the complete code. Copy the code below and save it in a file, say helloworld_pdf.php.


    <?php

    # helloworld_pdf


    // create a new pdf document

    $pdf = pdf_new();

    pdf_open_file($pdf, 'helloworld.pdf');


    // start a new page (Letter size)

    pdf_begin_page($pdf, 612, 792); // Letter size


    // setup font and print hello world

    pdf_set_parameter($pdf, 'FontOutline',

    'Arial=c:windowsfontsarial.ttf');

    $font = pdf_findfont($pdf, "Arial", "host", 1);

    pdf_setfont($pdf, $font, 48);

    pdf_show_xy($pdf, "hello world, PDF!", 50, 680);


    // done

    pdf_end_page($pdf);

    pdf_close($pdf);

    pdf_delete($pdf);


    echo "helloworld.pdf has been generatedn";

    ?>


    To run the code above, open a command window and cd to the directory where you saved the script. Suppose you have installed your PHP in c:php, and you have saved the script as helloworld_pdf.php. From the command prompt, type


    c:phpphp helloworld_pdf.php


    You should now have a new file named “helloworld.pdf” created in the folder where you run the script.

    Double click the pdf file. You will see the pdf document as shown below.




    Specifying the font information

    The most important line in this example is:


    pdf_set_parameter($pdf, 'FontOutline', 'Arial=c:windowsfontsarial.ttf');


    This is the "magic" line that specifies the font information. This is also the line that will fix the following error which many people have encountered:


    "PHP Fatal error:” Uncaught exception 'PDFlibException' with message 'Metrics data for font 'Arial' not found' "


    You can select any Windows truetype fonts on your system. For example, if you want to use Times New Roman Bold Italic, replace the codes with:


    pdf_set_parameter($pdf, 'FontOutline', ' Times New Roman Bold Italic =c:windowsfontsTIMESI.TTF');

    $font = pdf_findfont($pdf, "Times New Roman Bold Italic", "host", 1);


    Run the script again, and you will get the following PDF document:





     
     
    >>> More PHP Articles          >>> More By K.K.Sou
     

       

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