Feeds

Build your PDF and Excel reports with PHP

Office 2.0 without the fluff

Secure remote control for conventional and virtual desktops

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

The essential guide to IT transformation

More from The Register

next story
Apple promises to lift Curse of the Drained iPhone 5 Battery
Have you tried turning it off and...? Never mind, here's a replacement
Mozilla's 'Tiles' ads debut in new Firefox nightlies
You can try turning them off and on again
Linux turns 23 and Linus Torvalds celebrates as only he can
No, not with swearing, but by controlling the release cycle
Scratched PC-dispatch patch patched, hatched in batch rematch
Windows security update fixed after triggering blue screens (and screams) of death
This is how I set about making a fortune with my own startup
Would you leave your well-paid job to chase your dream?
prev story

Whitepapers

Endpoint data privacy in the cloud is easier than you think
Innovations in encryption and storage resolve issues of data privacy and key requirements for companies to look for in a solution.
Implementing global e-invoicing with guaranteed legal certainty
Explaining the role local tax compliance plays in successful supply chain management and e-business and how leading global brands are addressing this.
Advanced data protection for your virtualized environments
Find a natural fit for optimizing protection for the often resource-constrained data protection process found in virtual environments.
Boost IT visibility and business value
How building a great service catalog relieves pressure points and demonstrates the value of IT service management.
Next gen security for virtualised datacentres
Legacy security solutions are inefficient due to the architectural differences between physical and virtual environments.