Feeds

Build your PDF and Excel reports with PHP

Office 2.0 without the fluff

Boost IT visibility and business value

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. ®

Boost IT visibility and business value

More from The Register

next story
Apple fanbois SCREAM as update BRICKS their Macbook Airs
Ragegasm spills over as firmware upgrade kills machines
HIDDEN packet sniffer spy tech in MILLIONS of iPhones, iPads – expert
Don't panic though – Apple's backdoor is not wide open to all, guru tells us
Mozilla fixes CRITICAL security holes in Firefox, urges v31 upgrade
Misc memory hazards 'could be exploited' - and guess what, one's a Javascript vuln
NO MORE ALL CAPS and other pleasures of Visual Studio 14
Unpicking a packed preview that breaks down ASP.NET
Captain Kirk sets phaser to SLAUGHTER after trying new Facebook app
William Shatner less-than-impressed by Zuck's celebrity-only app
Cheer up, Nokia fans. It can start making mobes again in 18 months
The real winner of the Nokia sale is *drumroll* ... Nokia
EU dons gloves, pokes Google's deals with Android mobe makers
El Reg cops a squint at investigatory letters
Chrome browser has been DRAINING PC batteries for YEARS
Google is only now fixing ancient, energy-sapping bug
prev story

Whitepapers

Top three mobile application threats
Prevent sensitive data leakage over insecure channels or stolen mobile devices.
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.
Top 8 considerations to enable and simplify mobility
In this whitepaper learn how to successfully add mobile capabilities simply and cost effectively.
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.
The Essential Guide to IT Transformation
ServiceNow discusses three IT transformations that can help CIO's automate IT services to transform IT and the enterprise.