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

Security for virtualized datacentres

More from The Register

next story
Netscape Navigator - the browser that started it all - turns 20
It was 20 years ago today, Marc Andreeesen taught the band to play
Sway: Microsoft's new Office app doesn't have an Undo function
Content aggregation, meet the workplace ... oh
Sign off my IT project or I’ll PHONE your MUM
Honestly, it’s a piece of piss
Return of the Jedi – Apache reclaims web server crown
.london, .hamburg and .公司 - that's .com in Chinese - storm the web server charts
NetWare sales revive in China thanks to that man Snowden
If it ain't Microsoft, it's in fashion behind the Great Firewall
Chrome 38's new HTML tag support makes fatties FIT and SKINNIER
First browser to protect networks' bandwith using official spec
Admins! Never mind POODLE, there're NEW OpenSSL bugs to splat
Four new patches for open-source crypto libraries
prev story

Whitepapers

Forging a new future with identity relationship management
Learn about ForgeRock's next generation IRM platform and how it is designed to empower CEOS's and enterprises to engage with consumers.
Cloud and hybrid-cloud data protection for VMware
Learn how quick and easy it is to configure backups and perform restores for VMware environments.
Three 1TB solid state scorchers up for grabs
Big SSDs can be expensive but think big and think free because you could be the lucky winner of one of three 1TB Samsung SSD 840 EVO drives that we’re giving away worth over £300 apiece.
Reg Reader Research: SaaS based Email and Office Productivity Tools
Read this Reg reader report which provides advice and guidance for SMBs towards the use of SaaS based email and Office productivity tools.
Security for virtualized datacentres
Legacy security solutions are inefficient due to the architectural differences between physical and virtual environments.