Convert HTML text to Word

I am pulling out data from a DB that has HTML tagging around it since it comes from a GUI text editor, CKeditor to be specific (not that it really matters).
I first use this to build a PDF which is no problem.
Now I need to use this data to build a word document.  What I am doing is using an .rtf document  as the template since I have my headers and footers in there. Then in the .rtf template I have each of my variables to populate in there as %variableName%
I can't seem to format the text properly before this goes into the .rtf document and the HTML just becomes plain text and is entered that way.

Here is the code I am using.  But I need to have a good way of formatting the data which has HTML tagging around it before it is populated into the .rtf template:
<cfset pathToRTF =  GetDirectoryFromPath(GetCurrentTemplatePath()) & "MyTemplate.rtf" />

<cfset rtf = FileRead(pathToRTF) />
<cfset rtf = Replace(rtf,"%title%",title) />
<cfset rtf = Replace(rtf,"%variableName1%",variableName1) />
<cfset rtf = Replace(rtf,"%variableName2%",variableName2) />
<cfset rtf = Replace(rtf,"%variableName3%",variableName3) />

<cfheader name="content-disposition" value="filename=MyNewDocument.doc" />

<cfcontent type="application/msword">
<cfoutput>#rtf#</cfoutput>

Open in new window


Any help appreciated.
LVL 2
WebStalkersAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hammo777Commented:
Is CFDocument not an option?
Hammo777Commented:
Ignore that.  I though you could use CFDcoument to convert to Word if you had open office.  Its the other way around.
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Hammo777Commented:
It looks like you are doing everything right, so I assume the problem lies in the contents of the variables or the template itself.  I would be glad to take a look at them if you want to attach them.  To troubleshoot I would take the contents of the RTF, put it in a <cfsavecontent> block, manually paste in the contents of the variables and output that with your cfcontent and cfheader.  Play around with it until you get the desired results and then you will know what you need to reformat in your variables from the database.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WebStalkersAuthor Commented:
Thanks for the feedback on this.  It all comes down to the data stored in the variable (html) that isn't being converted properly before it gets passed to the .rtf template.
I'll keep playing with it some more but if you or anyone else has any other ideas please let me know.

Thanks!
_agx_Commented:
(no points. just a few suggestions ..)

I could be wrong, but I think you're trying to do something that's not possible. IIRC, RTF format doesn't understand html.  So any html will be treated as plain text.  

Normally I'd suggest using plain html, w/cfheader to force MS Word to treat it as a word document.  But unfortunately, creating headers and footers aren't quite so easy to do.
http://mvark.blogspot.com/2007/02/how-to-add-header-or-footer-to.html

WebStalkersAuthor Commented:
I've looked around on this...a LOT!  and im pretty sure your right.  This really isnt possible.  Unless I were to route this through something like a third party application that would be able to translate this.
Thanks for the help on this.
WebStalkersAuthor Commented:
There really was no end solution but this helped confirm that what im asking for here isnt really possible.
Hammo777Commented:
You probably should award aqx the points if you agree it isn't dooable.  I assumed you were treating the RTF file like a text file and that it contained html.  It is possible to take a file containing HTML, replace placeholders with more html from variables, and use cfcontent and cfheader to open it (at least I think so!)
_agx_Commented:
It is possible to take a file containing HTML, replace placeholders with more html from variables, and use cfcontent and cfheader to open it (at least I think so!)

Yeah, it is.  

Just to be clear, the part that's not possible is adding html to an rtf file.  AFAIK, rtf doesn't understand html, so any html tags will not be interpreted. What is possible is using plain html to generate a formatted file that MS Word can open.  Unfortunately, creating headers and footers are tricky. So if you need those too, well ... that might be the deal breaker.

Simple example of formatted doc
<cfheader name="Content-Disposition" value="attachment; filename=myFileName.doc">
<cfcontent type="application/msword" reset="true">
<h1>Psuedo Word Document</h1>
<div>This is html that <strong>MS Word</strong> can display

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ColdFusion Language

From novice to tech pro — start learning today.