4

I read some articles about the conversion of html to .docx and I found out that docx4j gives pretty decent results. I wonder if anyone could provide me the following info:

  1. Needed jars and versions.
  2. Sample code for conversion from html to .docx.

Sorry I couldn't post anything I tried because I haven't tried anything on this task yet, although I use Apache POI to convert the bytes[] I get from datatabse to html to output in a rich text editor on a jsf application. Please enlighten me, I'm lost in stress and confusion...!

1 Answer 1

4

To import XHTML, use

<dependency>
    <groupId>org.docx4j</groupId>
    <artifactId>docx4j-ImportXHTML</artifactId>
    <version>3.0.0</version>
</dependency>

See further http://www.docx4java.org/blog/2013/11/docx4j-3-0-and-maven/

For sample code, see https://github.com/plutext/docx4j-ImportXHTML/tree/master/src/samples/java/org/docx4j/samples

Note that your input needs to be well-formed XML, so if you have HTML, you'll need to tidy it first (with one of the many java libraries which can do this for you).

9
  • i tried this and i get this error : java.lang.NoClassDefFoundError: org/docx4j/org/xhtmlrenderer/render/Box Dec 9, 2013 at 10:38
  • 1
    That would suggest you didn't add the jar to your classpath successfully! Dec 9, 2013 at 19:18
  • true, the problem is I'm using the IDE project not maven feature, so i can't get all the working versions of the dependencies :/ please help ? Dec 9, 2013 at 22:39
  • Which IDE? You can download docx4java.org/docx4j/docx4j-3_0.zip Dec 10, 2013 at 6:33
  • yes yes, i downloaded the zip and its working fine now, thank you. Dec 10, 2013 at 8:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.