Feeds

Build your PDF and Excel reports with PHP

Office 2.0 without the fluff

High performance access to file storage

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

High performance access to file storage

More from The Register

next story
Windows 8.1, which you probably haven't upgraded to yet, ALREADY OBSOLETE
Pre-Update versions of new Windows version will no longer support patches
Android engineer: We DIDN'T copy Apple OR follow Samsung's orders
Veep testifies for Samsung during Apple patent trial
OpenSSL Heartbleed: Bloody nose for open-source bleeding hearts
Bloke behind the cockup says not enough people are helping crucial crypto project
Microsoft lobs pre-release Windows Phone 8.1 at devs who dare
App makers can load it before anyone else, but if they do they're stuck with it
Half of Twitter's 'active users' are SILENT STALKERS
Nearly 50% have NEVER tweeted a word
Windows XP still has 27 per cent market share on its deathbed
Windows 7 making some gains on XP Death Day
Internet-of-stuff startup dumps NoSQL for ... SQL?
NoSQL taste great at first but lacks proper nutrients, says startup cloud whiz
US taxman blows Win XP deadline, must now spend millions on custom support
Gov't IT likened to 'a Model T with a lot of things on top of it'
Microsoft TIER SMEAR changes app prices whether devs ask or not
Some go up, some go down, Redmond goes silent
prev story

Whitepapers

Securing web applications made simple and scalable
In this whitepaper learn how automated security testing can provide a simple and scalable way to protect your web applications.
Five 3D headsets to be won!
We were so impressed by the Durovis Dive headset we’ve asked the company to give some away to Reg readers.
HP ArcSight ESM solution helps Finansbank
Based on their experience using HP ArcSight Enterprise Security Manager for IT security operations, Finansbank moved to HP ArcSight ESM for fraud management.
The benefits of software based PBX
Why you should break free from your proprietary PBX and how to leverage your existing server hardware.
Mobile application security study
Download this report to see the alarming realities regarding the sheer number of applications vulnerable to attack, as well as the most common and easily addressable vulnerability errors.