Skip to main content
Inspiring
April 20, 2009
Question

Convert Word RTF to PDF with ColdFusion

  • April 20, 2009
  • 3 replies
  • 7049 views

I have thousands and thousands of rtf documents that need to be converted to pdf. Anybody like to tell me how to do this in ColdFusion 8?

This topic has been closed for replies.

3 replies

Inspiring
April 26, 2009

the iText project is improvng this as we speak, cfsearching's got a good blog post on it: http://cfsearching.blogspot.com/2009/04/itext-preview-of-things-to-come-someday.html

BKBK
Community Expert
Community Expert
April 25, 2009

<!--- source:http://livedocs.adobe.com/coldfusion/7/htmldocs/wwhelp/wwhimpl/common/html/wwhelp.htm?context=ColdFusion_Documentation&file=00001585.htm --->

<cflock scope="Application" type="exclusive" timeout="120"

>

<cfif not StructKeyExists(application, "MyWordObj")

>

<!--- First try to connect to an existing Word object --->

<cftry>

<cfobject type=

"com"

action=

"connect"

class=

"Word.application"

name=

"Application.MyWordobj"

context="local">

<cfcatch>

<!--- There is no existing object, create one --->

<cfobject type=

"com"

action=

"Create"

class=

"Word.application"

name=

"Application.MyWordobj"

context="local">

</cfcatch>

</cftry>

<cfset Application.mywordobj.visible = False

>

</cfif>

</cflock>

<!--- Convert a Word document in temp.doc to an HTML file in temp.htm. --->

<!--- Because this example uses a fixed filename, multiple pages might try

to use the file simultaneously. The lock ensures that all actions from

reading the input file through closing the output file are a single "atomic"

operation, and the next page cannot access the file until the current page

completes all processing.

Use a named lock instead of the Application scope lock to reduce lock contention. --->

<cflock

name="WordObjLock" type="exclusive" timeout="120"

>

<cfset docs = application.mywordobj.documents()

>

<cfset docs.open("c:\ColdFusion8\wwwroot\RTFtoPDF\temp.rtf")

>

<cfset converteddoc = application.mywordobj.activedocument

>

<!--- Val(8) works with Word 2000. Use Val(10) for Word 97 --->

<cfset converteddoc.saveas("c:\ColdFusion8\wwwroot\RTFtoPDF\temp.htm",val(8))

>

<cfset converteddoc.close()

>

</cflock>

<!--- Read the HTML file --->

<cffile

action="read" file="#expandPath('temp.htm')#" variable="fileToConvert"

>

<!--- Convert from HTML to PDF--->

<cfdocument

overwrite="yes" filename="#expandPath('temp.pdf')#" format="PDF"><cfoutput>#fileToConvert#

</cfoutput></cfdocument>

<p>Conversion from temp.rtf to temp.pdf complete

</p>

DinghusAuthor
Inspiring
April 25, 2009

Thank you. My problem is that there is no Word on the server itself.

Guess I will have to see what I can do about that.

BKBK
Community Expert
Community Expert
April 26, 2009

Dinghus wrote:

Thank you. My problem is that there is no Word on the server itself.

I don't think you require Word on the server. Just run the code and see.

Inspiring
April 23, 2009

A conversion like the one you describe is probably best handled by batch conversion software that can make the RTF to PDF conversion.  ColdFusion probably won't be the best choice for you here - Although you can read in the contents of the RTF file using <cffile>, coldfusion won't know how to translate the RTF format into HTML formatting needed to generate a PDF via <cfdocument>.  If you absolutely had to use coldfusion to complete the project, you could look into using Java packages (maybe iText?) to perform the task, but I'm not sure if libraries exist out there that do what you are asking.  However, since there are specific software applications written specifically for this sort of thing, you can probably find inexpensive software that has the functionality that you want.