Word Documents with PHP

I’ve just had the need to automatically generate word documents for the [FAS website] and I have to say I was surprised quite how simple that was with PHP. Well its kinda a fudge in that all I do is write HTML that openoffice understands and set the doctype to msword… does the trick nicely and here is a quick example ([inspiration for this]:
$filename =”yourdoc.doc”;
$fp = fopen($filename, “w”);
$test = “here is something really important that must be in a word document – argh”;
fputs($fp, $test);
fclose($fp);
header(‘Content-disposition: attachment; filename=$filename);
header(“Content-Type: application/msword”);
readfile($filename);
Of course I was extracting the data from a mysql database and parsing that into the document – quite please with that really.
Oh and page breaks when making .doc files in php are useful:
<p style=”page-break-before: always” >

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: