Home arrow PHP arrow Page 5 - Generate PDF Documents with PHP on the Windows Platform

Producing a simple bar graph in a PDF document - PHP

If you're a PHP programmer who has tried to generate PDF documents on the Windows platform, you may have run into some very irritating issues. This article will discuss those issues, show you how to generate PDF documents that can be viewed on a desktop and through a browser, and help you troubleshoot if you run into problems.

  1. Generate PDF Documents with PHP on the Windows Platform
  2. Hello world PDF
  3. Finding the filename of the truetype font
  4. Generating PDF documents for browsers
  5. Producing a simple bar graph in a PDF document
By: K.K.Sou
Rating: starstarstarstarstar / 9
September 16, 2008

print this article



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.


# 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',


$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);


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


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




// send it to browser

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

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



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: