TIP - Creating 'neat' Word documents with PHP

From: Date: Sun, 15 Jun 2003 14:34:22 +0000
Subject: TIP - Creating 'neat' Word documents with PHP
Groups: php.windows 
Request: Send a blank email to php-windows+get-20326@lists.php.net to get a copy of this message
Hi all, I had a requirement to produce neat Word documents to send as email attachments. I looked at the COM method but couldn't get it to work (didn't really try that hard though). Anyhow, I tried another method which procuces exactly what I need. Firstly, create the document shell in Word (this will possibly work in other formats but not tested here), leaving a space for any 'variable' content. Save the document as an RTF file (Rich Text Format). Open the RTF file in Notepad (or similar) and find the first part of your file (beginning of file to where the first variable goes in) and save that as a text file (filename1.txt), Then find the part between the first and second variables and save as 'filename2.txt' repeating until you have the whole rtf file saved as a collection of txt files. Store them on your server. In your php script do the following: $name="Stephen"; // my example $fp = "c:\\inetpub\\wwwroot\\mydir\\"; $fn1 = file_get_contents("letter1.txt"); $fn2 = file_get_contents("letter2.txt"); $fn3 = file_get_contents("letter3.txt"); $filename = "letter.doc"; $content = $fn1; $content.= date("d, M Y"); $content.= $fn2; $content.= $name; $content.= $fn3; if (!$fp = fopen($filename, 'w')) { print "Cannot open file ($filename)"; exit; } if (!fwrite($fp, $content)) { print "Cannot write to file ($filename)"; exit; } fclose($fp); I use phpmailer and I've tested the attachment part and it works fine. It might not suit every requirement but its a quick way to get letters formatted in word. Hope this helps someone. George in Oxford

« previous php.windows (#20326) next »