Leave That Thing Alone Blog
Using ColdFusion to convert RTF to (X)HTML
Here's a basic example of how to convert RTF to HTML with ColdFusion using Majix (http://sourceforge.net/projects/majix/). Majix is an java compliant open source project that converts RTF files to XML. Once the RTF has been converted to XML it''s easy to convert it to HTML with ColdFusion using XmlTransform.
I've had an ongoing struggle with a third party application that stores almost 4000 text municipal code entries as RTF. This frequently updated text needs to be displayed on the web as HTML with all the paragraphs, tables, bullets, etc displaying accurately. Using Majix has solved this problem.
The code below reads an RTF file and outputs the converted HTML text to the browser:
//set location of file(s) to convert
fileLocation = "c :\coldfusion7\wwwroot\majixtest\";
// create array of input and output filenames
filenameArray = arrayNew(1);
filenameArray[1] = "#fileLocation#\test.rtf"; //input
filenameArray[2] = "#fileLocation#\test.xml"; //output
// create Majix Java Object
majix = createobject("java","com.tetrasix.majix.MajixBatch");
// call Majix main method create the XML file
createXML = majix.main(filenameArray);
</cfscript>
<!--- read xml and xsl file --->
<cffile action="read" file="#fileLocation#\mydoc.xsl" variable="xmltrans">
<cffile action="read" file="#fileLocation#\test.xml" variable="xmldoc">
<!--- replace internal xsl and dtd references that majix adds by default --->
<cfset xmldoc =rereplacenocase(xmldoc,"<\?xml:stylesheet[^>]*>","")>
<cfset xmldoc =rereplacenocase(xmldoc,"<\!DOCTYPE mydoc PUBLIC[^>]*>","")>
<!--- output file --->
<cfoutput>#XmlTransform(xmldoc, xmltrans)#</cfoutput>
Example input RTF
Example output HTML
I modified an XSL file that is included with the Majix download. I had to make a couple small changes just to get the output XHTML valid
Majix requires an input RTF file, so if you have RTF in a database you will need to use cffile to write that data to a file first.
Download example
You will need to download Majix and make sure the \majix\lib\ is added to the ColdFusion Class Path

Comments
btw when i tried to submit this comment, your webserver asked for user/password.
Looks like a couple of my old posts are also now redisplayed by MXNA, sorry...
Very handy entry though, thanks for sharing it.
Thanks!