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

Hello world PDF - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

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