Build your PDF and Excel reports with PHP

Office 2.0 without the fluff

Internet Security Threat Report 2014

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

Internet Security Threat Report 2014

More from The Register

next story
New 'Cosmos' browser surfs the net by TXT alone
No data plan? No WiFi? No worries ... except sluggish download speed
iOS 8 release: WebGL now runs everywhere. Hurrah for 3D graphics!
HTML 5's pretty neat ... when your browser supports it
Mathematica hits the Web
Wolfram embraces the cloud, promies private cloud cut of its number-cruncher
Mozilla shutters Labs, tells nobody it's been dead for five months
Staffer's blog reveals all as projects languish on GitHub
'People have forgotten just how late the first iPhone arrived ...'
Plus: 'Google's IDEALISM is an injudicious justification for inappropriate biz practices'
SUSE Linux owner Attachmate gobbled by Micro Focus for $2.3bn
Merger will lead to mainframe and COBOL powerhouse
iOS 8 Healthkit gets a bug SO Apple KILLS it. That's real healthcare!
Not fit for purpose on day of launch, says Cupertino
Netscape plugins about to stop working in Chrome for Mac
Google kills off 32-bit Chrome, only on Mac
prev story


Secure remote control for conventional and virtual desktops
Balancing user privacy and privileged access, in accordance with compliance frameworks and legislation. Evaluating any potential remote control choice.
Intelligent flash storage arrays
Tegile Intelligent Storage Arrays with IntelliFlash helps IT boost storage utilization and effciency while delivering unmatched storage savings and performance.
WIN a very cool portable ZX Spectrum
Win a one-off portable Spectrum built by legendary hardware hacker Ben Heck
High Performance for All
While HPC is not new, it has traditionally been seen as a specialist area – is it now geared up to meet more mainstream requirements?
Beginner's guide to SSL certificates
De-mystify the technology involved and give you the information you need to make the best decision when considering your online security options.