I have a string variable which contains formatted html text and I have to convert that into .doc file using apache-poi.
I got this solution by using docx4j for .docx file, but client wants the solution by using apache-poi that is html string to .doc and .docx conversion.
So how to convert html text string to .doc and .docx file from formatted html text string using apache-poi in spring boot?
Edit: solutions-
For Doc :
private String getDocHtmlText(byte[] contents)
throws FileNotFoundException, IOException, ParserConfigurationException, TransformerConfigurationException,
TransformerFactoryConfigurationError, TransformerException {
File file = new java.io.File("reportTemplate.doc");
FileUtils.writeByteArrayToFile(file, contents);
InputStream input = new FileInputStream(file);
HWPFDocument wordDocument = new HWPFDocument(input);
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
WordToHtmlConverter converter = new WordToHtmlConverter(doc);
converter.processDocument(wordDocument);
ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
DOMSource domSource = new DOMSource(converter.getDocument());
StreamResult streamResult = new StreamResult(output);
Transformer serializer = TransformerFactory.newInstance().newTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.METHOD, "html");
serializer.transform(domSource, streamResult);
} finally {
input.close();
output.close();
file.delete();
}
return output.toString();
}
For Docx:
private String getDocxHtmlText(byte[] contents) throws IOException, FileNotFoundException {
File file = new java.io.File("reportTemplate.docx");
FileUtils.writeByteArrayToFile(file, contents);
InputStream in = new FileInputStream(file);
XWPFDocument document = new XWPFDocument(in);
XHTMLOptions options = XHTMLOptions.create().URIResolver(new FileURIResolver(new File("word/media")));
OutputStream out = new ByteArrayOutputStream();
XHTMLConverter.getInstance().convert(document, out, options);
in.close();
out.close();
file.delete();
return out.toString();
}