Feeds

Build your PDF and Excel reports with PHP

Office 2.0 without the fluff

Build a business case: developing custom apps

Specify how text is to be rendered. A rendermode value of 0 fills the text and a rendermode value of 1 uses stroke text.

cpdf_set_text_rendering($cpdf, 1);

Add the text to the PDF document using cpdf_text( ). Text coordinates may be specified with cpdf_text(). The mode parameter in cpdf_text() specifies the unit length in postscript points. The orientation parameter specifies the rotation of the text in degrees. The alignmode parameter specifies the alignment of the text.

cpdf_text($cpdf,"Catalog PDF");

The alignmode parameter specifies alignment with respect to the (x,y) coordinates of the text. End the title section with cpdf_end_text( ).

cpdf_end_text($cpdf);

Next, add some text to the PDF document using cpdf_begin_text(), cpdf_text() and cpdf_end_text() functions. Text is added to a new line using cpdf_continue_text( ). Create a table using cpdf_rect(). Add header row cells using cpdf_rect( ) and cpdf_stroke( ).

cpdf_rect($cpdf, 1.0, 6.0, 2.0, 0.5);
cpdf_stroke($cpdf);

Add text to the header cells. Specify the text position. Set text rendering to fill text. Set text font name, font size and font encoding. A row of data is added similar to a header row. A hyperlink may be added to row text using cpdf_set_action_url ( ). For example add an hyperlink to the title column data.

cpdf_set_action_url ( $cpdf, 7.20, 5.70, 8.5, 5.85, "http://www-128.ibm.com/developerworks/java/library/x-jaxpval.html");

End the PDF page using cpdf_finalize_page ( ). End the PDF document using cpdf_finalize ( )

cpdf_finalize_page($cpdf, 1);
cpdf_finalize($cpdf);

Set the Content-type header to application/pdf and save the PDF document to a file using cpdf_save_to_file ( ).

Header("Content-type: application/pdf");
cpdf_save_to_file($cpdf,"catalog.pdf");

The PHP script, catalog.php, used to generate an example PDF document is available in resources zip here. Run the catalog.php script with the URL http://localhost/catalog.php. You can see the PDF document, catalog.pdf in my screen shot. The hyperlinks are shown with blue rectangles in the Title column.

PDF Document generated with PHP.

Installing Spreadsheet_Excel_Writer

The Spreadsheet_Excel_Writer is available as a PEAR module. Therefore, first install the PEAR package Manager. Download the PEAR PHP file. and save the file as go-pear.php in the C:/PHP directory, the directory in which PHP 5 is installed. Install the PEAR Package Manager with the following command.

C:/PHP>php go-pear.php

The php.ini configuration file gets modified in installing the PEAR Package Manager. Therefore, restart the Apache2 HTTP server. Download Spreadsheet_Excel_Writer version 0.9.1. Save the zip file to the C:/PHP directory. The Spreadsheet_Excel_Writer has a required dependency, the PEAR OLE 0.5 package. Install the OLE 0.5 package with the following command.

C:/PHP>pear install channel://pear.php.net/OLE-0.5

Install the Spreadsheet_Excel_Writer with the following command.

C:/PHP>pear install Spreadsheet_Excel_Writer-0.9.1.tgz

Creating an Excel Spreadsheet

Create a PHP script, catalog-excel.php, in the C:\Apache2\\htdocs directory. Create a Workbook object. Specify filename as catalog.xls.

$workbook = new Spreadsheet_Excel_Writer('catalog.xls');

Add a worksheet to the workbook. If sheet name is not specified the sheet name is Sheeti, with i in [1..].

$worksheet =& $workbook->addWorksheet('Catalog');

Set page margins in inches. Center the page horizontally.

$worksheet->setMargins(0.25);
$worksheet->centerHorizontally(1);

Set the worksheet as the active worksheet.

$worksheet->activate();

First, we shall add a title to the worksheet. Add a format to the workbook for the title. Set the text to bold. Set the color of the cell's content to "blue". Set the font size to 25 pixels. Merge the cells in the row by setting the cell alignment to 'merge'.

$format_title =& $workbook->addFormat();
$format_title->setBold();
$format_title->setColor('blue');
$format_title->setSize(25);
$format_title->setAlign('merge');

Set the worksheet title to "Catalog Spreadsheet" in the first row and the third column using the Worksheet::write() function. The rows and columns in a worksheet are 0 indexed. The $format_title format that we created previously is used to add the title.

$worksheet->write(0, 2, 'Catalog Spreadsheet', $format_title);

Next, add a header row for a catalog. Add another format to the workbook for the header row. Set text to bold and font size to 15 pixels. Create a format for the data to be added to the worksheet. Set the font style to italic using Format::setItalic() and font size to 12 pixels. Set alignment of text to 'center' with Format::setAlign(). Horizontal alignment may be set to left, center, right, fill, justify, merge, or equal_space. Vertical alignment may be set to top, vcenter, bottom, vjustify, or vequal_space. A combination of horizontal and vertical alignment may be specified by invoking the setAlign() function more than once. Using the format for row data add a row of data with Worksheet::write(). The rows and columns are 0 indexed. Add a hyperlink to the Title column value with Worksheet::writeUrl(). The $url parameter specifies the URL of the hyperlink and the $string parameter specifies the label for the hyperlink.

Catalog-excel.php for creating an Excel spreadsheet is available in resources zip here. Run the PHP script with the URL http://localhost/catalog.php to create an Excel spreadsheet in the htdocs directory.

And you're out...

After all that you should now have an Excel spreadsheet that looks similar to my screen shot.

Excel Spreadsheet generated with PHP.

If you have, then you've successfully created a document in the business world's chosen medium for expressing information using one of the internet's hottest programming languages. ®

HP ProLiant Gen8: Integrated lifecycle automation

More from The Register

next story
KDE releases ice-cream coloured Plasma 5 just in time for summer
Melty but refreshing - popular rival to Mint's Cinnamon's still a work in progress
NO MORE ALL CAPS and other pleasures of Visual Studio 14
Unpicking a packed preview that breaks down ASP.NET
Secure microkernel that uses maths to be 'bug free' goes open source
Hacker-repelling, drone-protecting code will soon be yours to tweak as you see fit
Cheer up, Nokia fans. It can start making mobes again in 18 months
The real winner of the Nokia sale is *drumroll* ... Nokia
Put down that Oracle database patch: It could cost $23,000 per CPU
On-by-default INMEMORY tech a boon for developers ... as long as they can afford it
Another day, another Firefox: Version 31 is upon us ALREADY
Web devs, Mozilla really wants you to like this one
Google shows off new Chrome OS look
Athena springs full-grown from Chromium project's head
prev story

Whitepapers

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.
Consolidation: The Foundation for IT Business Transformation
In this whitepaper learn how effective consolidation of IT and business resources can enable multiple, meaningful business benefits.
Application security programs and practises
Follow a few strategies and your organization can gain the full benefits of open source and the cloud without compromising the security of your applications.
How modern custom applications can spur business growth
Learn how to create, deploy and manage custom applications without consuming or expanding the need for scarce, expensive IT resources.
Securing Web Applications Made Simple and Scalable
Learn how automated security testing can provide a simple and scalable way to protect your web applications.