O'Reilly logo

PHP Hacks by Jack D. Herrington

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Hack #48. Create RTF Documents Dynamically

Use PHP to generate Rich Text Format (RTF) documents dynamically.

Rich Text Format (RTF) is a text format used by word processors, notably Microsoft Word, and some text editors and viewers to store highly styled documents. If you want to generate documents dynamically with all of the features of a word processor, RTF gives you an opportunity to do that.

Start with a word processing document in an editor such as Microsoft Word. Figure 5-21 shows the document used in this hack.

The original Microsoft Word document

Figure 5-21. The original Microsoft Word document

The sections of the document with the %% markers around them are where I want the dynamic data to go. I could have picked any special characters, but %% has the advantage of being pretty distinct and unusual; further, the percent signs aren't encoded in RTF.

With the document in hand, use the Save As command to save the file as RTF. Then, using that RTF file as a template, you can start writing the PHP code that will generate the RTF.

The Code

Save the file shown in Example 5-39 as rtf.php.

Example 5-39. Using escape sequences to represent RTF

<? header( "content-type: application/msword" ); $customerName = "First customer"; $customerID = "cust_0001"; $data = array( array( trans => "123", desc => "Books", amount => '$123.25' ), array( trans => "345", desc => "Stamps", amount => '$22.93' ), array( trans => "1531", desc => "Candles", ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required