An oldie but a goodie, ColdFusion can be used to generate Word documents. Here is a quick example of writing to and opening a Word document.
Create Word Template
Create a Word template by adding “placeholders” in the document. This is done by wrapping a keyword with % (percent) signs as such: %firstname%. Put in as many as you want and add any necessary formatting in Word. Your replacement text will pick up the formatting. Save the file as an RTF (.rtf). For simplicity sake, put it in the same directory as the CF file used to generate it. Here an RTF file called homeworkpass.rtf is used.
Collect the Data
This example uses form fields to fill the document. Data from a database could be used just as easily. It’s a pretty standard form, nothing special.
<form action="index.cfm" method="post">
<fieldset>
<legend>Homework Pass Info</legend>
<p><label for="expirydate">Expiration Date:</label><br />
<input type="text" id="expirydate" name="expirydate" value="#DateFormat(DateAdd('d',10,Now()),'mm/dd/yy')#" /></p>
<p><label for="points">Points:</label><br />
<input type="text" id="points" name="points" value="100" /></p>
<p><label for="studentname">Student Name:</label><br />
<input type="text" id="studentname" name="studentname" /></p>
<p><label for="subject">Subject:</label><br />
<input type="text" id="subject" name="subject" /></p>
<p><label for="datereceived">Date Received:</label><br />
<input type="text" id="datereceived" name="datereceived" value="#DateFormat(Now(),'mm/dd/yy')#" /></p>
</fieldset>
<fieldset>
<legend>Version of ColdFusion:</legend>
<p><label><input type="radio" id="cfversion" name="cfversion" value="pre8" />CFMX or earlier</label><br />
<label><input type="radio" id="cfversion" name="cfversion" value="post8" />CF8 or later</label></p>
</fieldset>
<p><input type="submit" name="submit" value="Generate Word" /></p>
</form>
Replace Text with ColdFusion
After a little error checking, the file is opened and the placeholders are swapped out with the form field values using the Replace() function in the homeworkpass.rtf.
<cfset error = "" />
<cfif isDefined("form.submit")>
<cfloop collection="#form#" item="i">
<cfif NOT Len(form[i]) OR (NOT isDefined("form.cfversion"))>
<cfset error = "All fields must have a value" />
</cfif>
</cfloop>
<cfif NOT Len(error)>
<cfset pathToRTF = GetDirectoryFromPath(GetCurrentTemplatePath()) & "homeworkpass.rtf" />
<cflock name="homeworkpass" type="exclusive" timeout="30">
<cfif form.cfversion EQ "pre8">
<!--- CFMX7 or earlier --->
<cffile action="read" file="#pathToRTF#" variable="rtf">
<cfelse>
<!--- CF8 or later --->
<cfset rtf = FileRead(pathToRTF) />
</cfif>
<cfset rtf = Replace(rtf,"%expirydate%",form.expirydate) />
<cfset rtf = Replace(rtf,"%points%",form.points) />
<cfset rtf = Replace(rtf,"%studentname%",form.studentname) />
<cfset rtf = Replace(rtf,"%subject%",form.subject) />
<cfset rtf = Replace(rtf,"%datereceived%",form.datereceived) />
</cflock>
<cfheader name="content-disposition" value="filename=HomeworkPass.doc" />
<cfcontent type="application/msword"><cfoutput>#rtf#</cfoutput>
<cfabort>
</cfif>
</cfif>
There is a section here to allow for ColdFusion version specification. For ColdFusion 8 the FileRead() function can be used. All other versions of CF will need to use cffile.
<cfif form.cfversion EQ "pre8">
<!--- CFMX7 or earlier --->
<cffile action="read" file="#pathToRTF#" variable="rtf">
<cfelse>
<!--- CF8 or later --->
<cfset rtf = FileRead(pathToRTF) />
</cfif>
Offering a choice for ColdFusion will not be necessary in production; it’s here for illustrative purposes only. At this time all versions of Word can open .doc files.
Output the Word File
To output the Word file use cfcontent and stop processing the rest of the page.
<cfcontent type="application/msword"><cfoutput>#rtf#</cfoutput>
<cfabort>
To create multiple Word documents in a batch-like process see the Using ColdFusion to Generate Multiple Word Documents (Batch Creation) post.
If this post helped you out, please consider donating to help pay the hosting fees. 100% of the donations go to the web host.


Twitter
About
33 Comments
@Michael
RTFs are limited in their ability to format. And, I haven’t played around with special characters or HTML in them; I haven’t needed to. CF plays nicer with other Adobe products – imagine that – than it does with anything Microsoft.
Great post, however, if the placeholder document has a placeholder fed with input text from a Rich Text Editor, the ensuing output .RTF document shows raw html tags (such as etc).
Any tip on getting this html formatting to display properly once inside of the .RTF document?
I really like the idea of using RTF documents instead of DOCs. Great tip!
@Simon
Here you go. Let me know if it’s what you needed.
http://www.jensbits.com/2009/07/08/using-coldfusion-to-generate-multiple-word-documents-batch-creation/
Hi Jen,
You wouldn’t know of a simply way of modifying your code to handle multiple pages (e.g. if you wanted to produce a home work pass for all the students in a class)?
I’ve got the script working nicely to generate certificates for people who attend training courses, but I can only generate one certificate at a time at the moment.
Thank you very much for your assistance…
This post is very timely. Just what I was looking for. Thank you very much Jen!
)
@covayntc
Look through Word help. You may have to set the encoding on the Word document itself.
Please help me to solve problems: some of the textboxes of form have unicode characters, the outputted Word file can not display exactly. How to display unicode characters in Word file?
@Bryan
You’re welcome. Sometimes these one-off solutions are just what you need.
Thanks! This post really helped me out.