Subodh Joshi
Subodh Joshi

Reputation: 13482

How to convert HTML to a Microsoft Word document ?

How can I convert HTML from a CKEditor into a Microsoft Word document?

Upvotes: 1

Views: 11496

Answers (2)

nanosoft
nanosoft

Reputation: 3091

I wrote a java method to achieve it using api docx4j-ImportXHTML.

The idea is to create method which takes xhtml, resultant filename and path at which the new file to be saved. This file will process the xhtml and will save it as file with provided filename and at given destination. Check Full code link for complete code.

To add dependency docx4j-ImportXHTML, use(3.3.1 is latest version while I am writing this answer. If you seeing it later use latest stable version of your time).

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

Upvotes: 1

Joel Peltonen
Joel Peltonen

Reputation: 13402

CKEditor is just HTML, so what you really should be asking yourself is:

How do I save CKEditor contents as an HTML file and then convert that into a .doc file.

The answer to the first part should be trivial. Protip: You send the contents as a string to your backend and your backend writes that string to an .html file.

As for the conversion, you can try saving the content as a .html file, then opening that in Word and then saving it as a doc. Should be possible although Word is no browser and if your content is complicated, it will fail. You might have some luck automating the process with LibreOffice, which has a command line spell for this.

Also, this has been asked before, just not in this form: Converting HTML to doc(x) and / or PDF

Upvotes: 1

Related Questions