I am using docx4j jar for converting variables in a docx document and then converting it to pdf.
Below is the code
- Code: Select all
public File generateDocument(InputStream inputStream, Map<String, String> params, String fileName, String tempDir) {
OutputStream os = null;
byte[] bytes = null;
File file;
Map<String, String> fields;
try {
tempDir = correctDirectoryPath(tempDir);
bytes = IOUtils.toByteArray(inputStream);
file = new File(tempDir + fileName + this.formatter.format(new Date()) + ".pdf");
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new ByteArrayInputStream(bytes));
VariablePrepare.prepare(wordMLPackage);
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
if (params != null) {
fields = new HashMap<>(params);
documentPart.variableReplace((HashMap<String, String>) fields);
}
Mapper fontMapper = new IdentityPlusMapper();
wordMLPackage.setFontMapper(fontMapper);
List<SectionWrapper> sectionWrappers = replaceHeaders(params, wordMLPackage);
FieldUpdater updater = new FieldUpdater(wordMLPackage);
updater.update(true);
FOSettings foSettings = Docx4J.createFOSettings();
foSettings.setWmlPackage(wordMLPackage);
if (!file.exists()) {
file.createNewFile();
}
os = new FileOutputStream(file);
Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
System.out.println("Done!");
return file;
}
catch (Exception e) {
logger.error("Some error occurred while processing document", e);
return null;
}
finally {
try {
if (os != null) {
os.close();
}
if (inputStream != null) {
inputStream.close();
}
}
catch (IOException e) {
logger.error("Could not close input/output stream", e);
}
}
}
But when I run this, the bullets in the docx document get converted to # symbol in the pdf.
Attached are the template and the output file.
I am using 3.2.0 jar for docx4j.
Please help me understand what am I missing!!