Wednesday, July 1, 2009

ColdFusion: Experiment Converting MS Word to HTML/PDF - Part 2 Example

A brief addendum to Part 2. Here is an example of using a OpenXMLViewer.exe from a .bat file. Just update the directory to match the location of the OpenXMLViewer folder on your system.

Note, the cd (change directory) statement in the first line is important. If you have not added the OpenXMLViewer folder to your PATH environment variable, you must call the program from within the parent directory so it can locate the needed dependency files.

BAT File


@ECHO OFF
REM **********************************
REM %1 Full path to the source file *.docx
REM %2 Full path to output folder (ie directory not file name)
REM %3 Browser type (IE, FIREFOX or OPERA)
REM **********************************

cd % C:\tools\OpenXMLViewer\
OpenXMLViewer.exe %1 %2 %3


ColdFusion Code
<!---
Initialize file paths
--->
<cfset inputFilePath = ExpandPath("Introduction to Microsoft .NET Services.docx")>
<cfset outputFolder = ExpandPath("test")>
<cfset pathToProgram = ExpandPath("openXMLViewerConvert.bat")>
<cfset browserType = "FIREFOX">

<!---
OpenXMLViewer does not seem to create the output
folder if it does not exist. So ensure it exists
before doing the conversion.
--->
<cfif NOT DirectoryExists(outputFolder)>
<cfdirectory action="create" directory="#outputFolder#">
</cfif>

<!---
Do the conversion
arguments='/c "#pathToProgram#" "#inputFilePath#" "#outputFolder#" #browserType# 2>&1'
--->
<cfexecute name="c:\windows\system32\cmd.exe"
arguments='/c #pathToProgram# "#inputFilePath#" "#outputFolder#" #browserType# 2>&1'
timeout="120"
variable="result" />

<!---
Display the generated html file
--->
<cfdirectory action="list" directory="#outputFolder#" name="getHTMLFiles" filter="*html*">

<h3>Generated HTML</h3>
<cfoutput query="getHTMLFiles">
<a href="#ListLast(directory, '\/')#/#Name#">Display HTML (#Name#)</a>
</cfoutput>

<cfdump var="#result#" label="Results from Cfexecute">

0 comments:

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Header image adapted from atomicjeep