This article is more than 1 year old
Build your PDF and Excel reports with PHP
Office 2.0 without the fluff
Hands on The Portable Document Format (PDF) and Microsoft's Excel spreadsheet are commonly used for presentation of reports and data.
PHP, meanwhile, has become one of the most commonly used scripting languages on the web today, with 35 per cent of web sites running PHP. The TIOBE index of programming languages also indicates an increase in the usage of PHP.
Given the prevelance of PHP, Excel and PDF it's fortunate there exists class libraries for the generation of PDF documents and Excel spreadsheets using PHP. In my latest guide to PHP, I shall look at generating an Excel spreadsheet using the PHP Extension and Application Repository (PEAR) module, Spreadsheet_Excel_Writer
and the ClibPDF
PHP library to generate a PDF report. Along the way I'll dig into PDF and Excel report features such as setting fonts and adding a hyperlink.
Installing ClibPDF
First, install PHP 5 and Apache2 HTTP Server and configure the Apache server with PHP. We won't discuss configuring Apache server with PHP as it was discussed in an earlier article on PHP, Accessing DB2 UDB with PHP. The ClibPDF
PHP class library extension is included in the Collection of PECL modules for PHP 5.3. Extract the php_cpdf.dll
from the PECL modules zip file to the C:/PHP/ext
directory. Add the following PHP directive to the php.ini
configuration file.
extension=php_cpdf.dll
Restart the Apache HTTP server.
Creating a PDF Document with ClibPDF
Create a PHP file catalog.php
in the C:\Apache2\htdocs
directory, the document root directory of Apache2 server. In the PHP file, create a new PDF document using the cpdf_open ( )
function. If document compression is to be set, specify compression as a non 0 value. A file name may be specified to output the generated PDF document. If filename is not specified an in memory PDF document is created that may be output to a file or stdout.
$cpdf=cpdf_open(0);
Set the title of the document.
cpdf_set_title($cpdf, "Catalog PDF");
Start a new page using cpdf_page_init()
. Specify page number as 1, and page size as A4 (595x842). Set orientation to portrait (0)
. Orientation may also be set to landscape (1)
. The unit parameter is optional and specifies the number of postscript points per unit; the default value is 72, which corresponds to 1 inch.
cpdf_page_init($cpdf, 1, 0, 595, 842);
Add a bookmark for the current page. Specify the text of the bookmark in the text parameter.
cpdf_add_outline($cpdf, 0, 0, 0, 1, "Page 1");
Next, add title text of the PDF document. Start a text section with cpdf_begin_text ( )
. Set the font to Courier-Bold, font size to 25 and font encoding to WinAnsiEncoding
using cpdf_set_font ( )
. Encoding may be set to MacRomanEncoding
, MacExpertEncoding
, WinAnsiEncoding
or NULL
. If encoding is set to NULL
, the font's built-in encoding is used.
cpdf_set_font($cpdf, "Courier-Bold", 25, "WinAnsiEncoding");
Set the coordinates of the text with cpdf_set_text_pos ( )
. The mode
parameter specifies the unit length in postscript points. If mode is 0 or is omitted the default unit length is used.
cpdf_set_text_pos($cpdf,4.70,7.5);