Build your PDF and Excel reports with PHP

Office 2.0 without the fluff

Choosing a cloud hosting partner with confidence

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( ).


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

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

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

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.


Set the worksheet as the active worksheet.


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

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

Beginner's guide to SSL certificates

More from The Register

next story
Nexus 7 fandroids tell of salty taste after sucking on Google's Lollipop
Web giant looking into why version 5.0 of Android is crippling older slabs
Be real, Apple: In-app goodie grab games AREN'T FREE – EU
Cupertino stands down after Euro legal threats
Download alert: Nearly ALL top 100 Android, iOS paid apps hacked
Attack of the Clones? Yeah, but much, much scarier – report
Microsoft: Your Linux Docker containers are now OURS to command
New tool lets admins wrangle Linux apps from Windows
Bada-Bing! Mozilla flips Firefox to YAHOO! for search
Microsoft system will be the default for browser in US until 2020
prev story


Why and how to choose the right cloud vendor
The benefits of cloud-based storage in your processes. Eliminate onsite, disk-based backup and archiving in favor of cloud-based data protection.
A strategic approach to identity relationship management
ForgeRock commissioned Forrester to evaluate companies’ IAM practices and requirements when it comes to customer-facing scenarios versus employee-facing ones.
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.
Mitigating web security risk with SSL certificates
Web-based systems are essential tools for running business processes and delivering services to customers.
Storage capacity and performance optimization at Mizuno USA
Mizuno USA turn to Tegile storage technology to solve both their SAN and backup issues.