PHP
  Home arrow PHP arrow Page 5 - 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 - Producing a simple bar graph in a PDF document
    ( Page 5 of 5 )

    Now that you know the basics of generating a PDF document, you can start playing with the whole suite of PDFlib functions as listed in the PHP Manual.

    Below is another sample code that outputs a simple bar graph to a PDF document as shown below:




    I have sprinkled many comments in the code, so it should be quite self-explanatory.


    <?php

    # pdf_example4

    # a simple bar graph


    // create a new pdf document

    $pdf = pdf_new();

    $filename = 'c:pdf_example4.pdf';

    pdf_open_file($pdf, $filename);


    // start a new page (Letter size)

    pdf_begin_page($pdf, 612, 792);


    // 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, 16);

    pdf_show_xy($pdf, "Simple Bar Graph", 50, 720);


    // draw the x and y axis

    pdf_moveto($pdf, 50, 690);

    pdf_lineto($pdf, 50, 520);

    pdf_lineto($pdf, 400, 520);

    pdf_stroke($pdf);


    // draw the bar chart

    $x = 80;

    $y = 520;

    $w = 40;


    // the data and color for each column

    $data = array('120','160','300','240');

    $color = array('#4EC3BC', '#DAA876', '#E29CC8', '#FDE0C6');


    // get into some meat now, cheese for vegetarians;

    for ($i=0;$i<count($data);$i++){


    // calculate the height of the bar

    $y_ht = ($data[$i]/max($data))* 100;


    // set the color for each bar

    list($r, $g, $b) = hex2rgb($color[$i]);

    pdf_setcolor($pdf, "fill", "rgb", $r, $g, $b, 0);


    // draw the bar

    pdf_rect($pdf, $x, $y, $w, $y_ht);

    pdf_fill_stroke($pdf);


    // write the bar label

    pdf_setcolor($pdf, "fill", "rgb", 0, 0, 0, 0);

    pdf_setfont($pdf, $font, 10);

    pdf_show_xy($pdf, $data[$i], $x+12, $y-16);


    // go to the next bar

    $x = $x+$w+40;

    }


    // done

    pdf_end_page($pdf);

    pdf_close($pdf);

    pdf_delete($pdf);


    // send it to browser

    header("Content-type: application/pdf");

    header("Content-Disposition: inline; filename=pdf_example3.pdf");

    readfile($filename);

    unlink($filename);


    // function to convert a standard rgb from hex to float

    function hex2rgb($hex) {

    $color = str_replace('#','',$hex);

    $r = hexdec(substr($color,0,2))/255;

    $g = hexdec(substr($color,2,2))/255;

    $b = hexdec(substr($color,4,2))/255;

    return array($r, $g, $b);

    }


    ?>

    Final Words

    We’ve come to the end of this article. In this tutorial I’ve shown you how to generate PDF documents on the Windows platform. I’ve also given you a number of complete sample codes that should give you a head start in generating your own PDF documents.

    Take some time to explore that various PDFlib functions as listed in the PHP Manual. Just bear in mind that of some of the sample codes there might not work because they might have been written for higher versions of PDFlib.

    Have lots of fun generating PDF documents with PHP!



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