I'm looking for this for a while now but so far I have no clue how to achieve this. The thing is that I have dynamic html content produced and than using docx4j I convert it to docx. Docx elements look is acceptable at the moment but since there is significant amount of dynamic content populated with help of jstl tags the outcome is all over the place. Here how I do it:
first get page source:
- Code: Select all
String content = renderViewHelper.renderView("/report/testPrint2",model);
then I process this string:
@Override
public File createDocx(String content, ReportDto report, Entity entity,
ScpUser user) {
FileOutputStream fop = null;
String inputfilepath = "tempHtmlFile.html";
File tempHtmlFile = null;
File tempDocxFile = null;
try {
tempHtmlFile = File.createTempFile("tempHtmlFile", ".html");
tempDocxFile = File.createTempFile("report", ".docx");
// ensure we have UTF8 everywhere
fop = new FileOutputStream(tempHtmlFile);
Writer out = new BufferedWriter(new OutputStreamWriter(fop, "UTF8"));
out.write(content);
out.flush();
out.close();
String regex = null;
regex = ".*(arial|times).*";
PhysicalFonts.setRegex(regex);
// Document loading (required)
WordprocessingMLPackage wordMLPackage;
System.out.println("Loading file from " + inputfilepath);
// wordMLPackage = Docx4J.load(new java.io.File(inputfilepath));
HTMLSettings htmlSettings = Docx4J.createHTMLSettings();
htmlSettings.setImageDirPath(inputfilepath + "_files");
htmlSettings.setImageTargetUri(inputfilepath
.substring(inputfilepath.lastIndexOf("/") + 1) + "_files");
String userCSS = "html, body, div, span, h1, h2, h3, h4, h5, h6, p, a, img, ol, ul, li, table, caption, tbody, tfoot, thead, tr, th, td "
+ "{ margin: 0; padding: 0; border: 0;}"
+ "body {line-height: 1;} ";
htmlSettings.setUserCSS(userCSS);
wordMLPackage = WordprocessingMLPackage.createPackage();
RFonts arialRFonts = Context.getWmlObjectFactory().createRFonts();
arialRFonts.setAscii("Arial");
arialRFonts.setHint(org.docx4j.wml.STHint.DEFAULT);
arialRFonts.setHAnsi("Arial");
XHTMLImporterImpl.addFontMapping("Arial", arialRFonts);
RFonts timesRFonts = Context.getWmlObjectFactory().createRFonts();
timesRFonts.setAscii("Times");
timesRFonts.setHint(org.docx4j.wml.STHint.DEFAULT);
timesRFonts.setHAnsi("Times");
XHTMLImporterImpl.addFontMapping("Times New Roman", timesRFonts);
RFonts serifRFonts = Context.getWmlObjectFactory().createRFonts();
serifRFonts.setAscii("sans-serif");
serifRFonts.setHint(org.docx4j.wml.STHint.DEFAULT);
serifRFonts.setHAnsi("sans-serif");
XHTMLImporterImpl.addFontMapping("MS Sans Serif", serifRFonts);
XHTMLImporterImpl xHTMLImporter = new XHTMLImporterImpl(
wordMLPackage);
xHTMLImporter.setHyperlinkStyle("Hyperlink");
// xHTMLImporter.setParagraphFormatting(FormattingOption.IGNORE_CLASS);
wordMLPackage.getDocumentModel().getSections().get(0)
.getPageDimensions().setPgSize(PageSizePaper.A4, true);
wordMLPackage.getDocumentModel().getSections().get(0)
.getPageDimensions().setMargins(MarginsWellKnown.NARROW);
wordMLPackage.getMainDocumentPart().getContent()
.addAll(xHTMLImporter.convert(tempHtmlFile, null));
wordMLPackage.save(tempDocxFile);
System.out.println("Saved: " + "report.docx");
return tempDocxFile;
} catch (Docx4JException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
System.out
.println("Something went wrong during opening test.docx...");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
tempHtmlFile.delete();
if (fop != null) {
fop.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
Can anyone help me on this subject?
Best regards,
Mat