WordToHtmlRenditionProvider implements RenditionProvider { @Override public InputStream convert(InputStream fromInputSource, String toMimeType) { try { WordprocessingMLPackage pkg = WordprocessingMLPackage.load(fromInputSource); FieldUpdater updater = new FieldUpdater(pkg); updater.update(true); AbstractHtmlExporter exporter = new HtmlExporterNG2(); HTMLSettings htmlSettings = Docx4J.createHTMLSettings(); htmlSettings.setWmlPackage(pkg); Docx4jProperties.setProperty("docx4j.Convert.Out.HTML.OutputMethodXML", true); File tmpFile = Files.createTempFile(null, null, new FileAttribute<?>[]{}).toFile(); OutputStream os = new FileOutputStream(tmpFile); Docx4J.toHTML(htmlSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL); IOUtils.closeQuietly(os); if (pkg.getMainDocumentPart().getFontTablePart() != null) { pkg.getMainDocumentPart().getFontTablePart().deleteEmbeddedFontTempFiles(); } htmlSettings = null; pkg = null; return new ObservableInputStream(new FileInputStream(tmpFile), new FileRemover(tmpFile)); } catch (Exception e) { logger.warn(format("%s rendition failed", toMimeType), e); } return null; }  @Override String consumes(); @Override String[] produces(); @Override InputStream convert(InputStream fromInputSource, String toMimeType);  }
@Test public void testConvert() throws Exception { InputStream converted = service.convert(this.getClass().getResourceAsStream("/sample-docx.docx"), "text/html"); assertThat(converted.available(), is(greaterThan(0))); assertThat(((ObservableInputStream)converted).getObservers(), hasItem(is(instanceOf(FileRemover.class)))); Document doc = Jsoup.parse(converted, "UTF8", "http: Elements htmls = doc.getElementsByTag("HTML"); assertThat(htmls.size(), is(1)); Element html = htmls.get(0); assertThat(html, is(not(nullValue()))); }