Creating a DOCX

I am working on a POC where I need to create a DOCX from some plain text. I was wondering if anybody had any advice on how to do this with Lucee. It appears the CFDOCUMENT won’t do it.

ChatGPT recommended docx4j , has anybody had success with this?

Thanks

Windows Server 2022
IIS 10
Lucee: 6.2.1.122
Tomcat/10.1.36
Java|21.0.6 (Eclipse Adoptium) 64bit

if it’s just plain text, from memory, word will read a html file as a doc

Are you by any chance already using the Spreadsheet CFML library? If so you can also use its underlying POI libraries to write your own Word generation fairly easily. Use the createJavaObject method to work with the classes you need.

spreadsheet = New spreadsheetCFML.Spreadsheet()
resultPath = ExpandPath( "simple.docx" )
try{
  doc = spreadsheet.createJavaObject( "org.apache.poi.xwpf.usermodel.XWPFDocument" )
  paragraph = doc.createParagraph()
  textRun = paragraph.createRun()
  textRun.setText( "Hello World" )
  outputStream = CreateObject( "java", "java.io.FileOutputStream" ).init( resultPath )
  doc.write( outputStream )
}
finally{
  if( !IsNull( doc ) )
    doc.close()
  if( !IsNull( outputStream ) )
    outputStream.close()
}
1 Like

…Or, since you’re on Lucee 6.2 you could give the new Maven integration a spin!

poi = New component javaSettings='{
  "maven": [
    "org.apache.poi:poi:5.4.1",
    "org.apache.poi:poi-ooxml:5.4.1",
    "org.apache.poi:poi-ooxml-full:5.4.1",
    "org.apache.xmlbeans:xmlbeans:5.3.0"
    ]
  }' {

  import "org.apache.poi.xwpf.usermodel.*"
  import "java.io.FileOutputStream"

  function createDocx( required path, string text="" ){
    try{
      var doc = New XWPFDocument()
      var paragraph = doc.createParagraph()
      var textRun = paragraph.createRun()
      textRun.setText( arguments.text )
      var outputStream = New FileOutputStream( arguments.path )
      doc.write( outputStream )
    }
    finally{
      if( !IsNull( doc ) )
        doc.close()
      if( !IsNull( outputStream ) )
        outputStream.close()
    }
  }

}//end component
resultPath = ExpandPath( "simple.docx" )
poi.createDocx( resultPath, "Hello World!" )

I think I’m pregnant. #lmao

1 Like

My installation is more or less out of the box right now. I’m not familiar with Maven.

I could not get the code to work. I have Grok play with it and ended up with the two attached files, but just can’t get it working. It’s suggesting “Since Lucee is failing to load xmlbeans:5.3.0 (and potentially other dependencies), we’ll download the required JARs and place them in Lucee’s library directory.”

It listed a LOT of files.
createdoc.cfm (866 Bytes)
PoiComponent.cfc (3.0 KB)

What I am looking to do is take a (Json format) response from an LLM and turning the content into a Word DOCX

Your code worked fine for me, once I removed the unnecessary var from line 19 of createdoc.cfm

PoiComponent initialized
Result path: C:\dev\test\docx\simple.docx
Document created successfully at C:\dev\test\docx\simple.docx

Does the following folder lucee\tomcat\lucee-server\mvn exist? If so what does it contain?

The issue may be because you are using CreateObject() rather than the import aliases as in my example.

Try this amended version of your component:

PoiComponent.cfc (2.8 KB)

1 Like

wouldn’t this make a great lucee extension for 6.x/7x #winkwink

Thanks @Julian_Halliwell I tried it but it threw an error
PoiComponent initialized
Result path: D:\ACS\aspirenet\test\simple.docx
Error creating document: Failed to create document: Cannot load XWPFDocument class: could not find component or class with name [XWPFDocument]

My MVN folder has various sub-folders, screenshot attached

mvn

try reloading the page a few times…

https://luceeserver.atlassian.net/browse/LDEV-5601

If you drill down, do you have lucee\tomcat\lucee-server\mvn\org\apache\poi\poi-ooxml\5.4.1\poi-ooxml-5.4.1.jar?

That’s the jar that contains the XWPFDocument class.

Yep, the JAR is there, screenshot attached

@Zackster I reloaded the page a half dozen times, no change.

OK, is it possible you’ve got POI anywhere else in your installation?

I’ve modified your files to report the POI version being loaded. Can you try that?

createdoc.cfm (922 Bytes)
PoiComponent.cfc (2.9 KB)

This component java integration is quite new and I’m finding some aspects of it a bit flaky at the moment, so failing that, I would give my first suggestion a go, using Spreadsheet CFML. No installation needed, just copy the required files/folders to your app and call it as above. No need to mess about with the POI jars, it’s all packaged up and loaded for you.

Thanks again. I tried those files, it failed
Lucee 6.2.1.122 Error (expression)
Message Component [PoiComponent] has no function with name [getVersion]
AI (Experimental) For AI-driven exception analysis setup, see AI Setup Guide.
Stacktrace The Error Occurred in
D:\ACS\aspirenet\test\createdoc.cfm: line 13

11: }
12:
13: dump( var=poi.getVersion(), label=“POI version” );
14:
15: // Define the result path

Park everything for now. I just noticed a DOCX in the folder, despite all the errors and a (2) version of one of the CFMs

Your last files are working.

I have my Hello World!

Now I just need to understand, how do I put line breaks in there and can I control any of the basic formattting?

1 Like