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...!

FranciscoBouza
  • 590
  • 6
  • 19
Jalal Sordo
  • 1,605
  • 3
  • 41
  • 68

1 Answers1

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).

JasonPlutext
  • 15,352
  • 4
  • 44
  • 84
  • i tried this and i get this error : java.lang.NoClassDefFoundError: org/docx4j/org/xhtmlrenderer/render/Box – Jalal Sordo Dec 09 '13 at 10:38
  • 1
    That would suggest you didn't add the jar to your classpath successfully! – JasonPlutext Dec 09 '13 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 ? – Jalal Sordo Dec 09 '13 at 22:39
  • Which IDE? You can download http://www.docx4java.org/docx4j/docx4j-3_0.zip – JasonPlutext Dec 10 '13 at 06:33
  • yes yes, i downloaded the zip and its working fine now, thank you. – Jalal Sordo Dec 10 '13 at 08:56
  • i wonder why they down voted me on this question, as you understood me and gave me the perfect answer :) – Jalal Sordo Mar 06 '14 at 19:12
  • Papa_Jay I am getting similar exception. I have following jars in classpath docx4j-3.1.0.jar,docx4j-ImportXHTML-3.0.0.jar,slf4j-nop-1.7.7.jar, slf4j-api-1.7.7.jar. Can tell me what were the jars you included in classpath ? – Ankit Jul 28 '14 at 14:05
  • You should probably create your own question, but you maybe missing org.docx4jxhtmlrenderer. Unless you know what you are doing, its best to include all dependencies. – JasonPlutext Jul 28 '14 at 23:23
  • check full code : http://stackoverflow.com/a/41937582/1406510 – nanosoft Jan 30 '17 at 13:57