docx-stamper
docx-stamper copied to clipboard
Possible to use custom function on expression inside repeat table?
I'm trying to call a custom function on an expression inside the repeat table and it doesn't work. According to the document, "Expressions found in the cells of the table row are evaluated against the object from the list". It can be inferred from the quote that in my case, the expression is evaluated against Map<String,String>
in which the custom function is not present. Am i missing something or this feature hasn't been implemented yet?
Here are the codes:
public interface DateFormatFunction {
String formatDate(final String dateString, final String format) throws ParseException;
}
public static class DateFormatFunctionImpl implements DateFormatFunction {
private static final String INPUT_FORMAT = "dd/MM/YYYY";
@Override
public String formatDate(final String inputDateString, final String outputFormat) throws ParseException {
final SimpleDateFormat inputFormatter = new SimpleDateFormat(INPUT_FORMAT);
final Date inputDate = inputFormatter.parse(inputDateString);
final SimpleDateFormat outputFormatter = new SimpleDateFormat(outputFormat);
return outputFormatter.format(inputDate);
}
}
public static class TemplateContext {
private List<Map<String, String>> items;
public List<Map<String, String>> getItems() {
return items;
}
public void setItems(List<Map<String, String>> items) {
this.items = items;
}
}
@Test
public void testMap2() throws FileNotFoundException, IOException {
TemplateContext context = new TemplateContext();
List<Map<String, String>> csvRecords = parseCSV(new File("sample_1.csv"));
context.setItems(csvRecords);
try (FileInputStream inputStream = new FileInputStream("report_template.docx")) {
DocxStamper<TemplateContext> stamper = new DocxStamperConfiguration()
.exposeInterfaceToExpressionLanguage(DateFormatFunction.class, new DateFormatFunctionImpl())
.build();
final ByteArrayOutputStream reportOutputStream = new ByteArrayOutputStream();
stamper.stamp(inputStream, context, reportOutputStream);
final ByteArrayInputStream reportInputStream = new ByteArrayInputStream(reportOutputStream.toByteArray());
try (FileOutputStream pdfFileOutputStream = new FileOutputStream("report_result.pdf")) {
convertDocxToPdf(reportInputStream, pdfFileOutputStream);
}
}
}
My template:
Thanks, Le
Problem still present in 1.4.0
I updated the test RepeatTableRowTest on a fork branch to reproduce the issue
https://github.com/alvinmeimoun/docx-stamper/tree/issue/60/reproduce
EDIT : Made a workaround https://github.com/alvinmeimoun/docx-stamper/tree/issue/60/workaround I didn't made a PR because i'm not 100% confident of my implementation. And with my implementation all stamped classes must have an empty constructor so it can break source for some users.
If i find some free time i will check more but this workaround will unblock my actual project and i will use it until a better fix will be merged and released
Nice work @alvinmeimoun. This seems to have fixed the bug. With your changes, expressions are now working within repeat sections.
Thank you @alvinmeimoun for providing that workaround! Works for me, too! I would love if this bug was adressed in the master branch so I don't have to rely on a locally installed jar 😃
@alvinmeimoun, @KasparScherrer, @vegemite4me we'll take care of this subject when possible in our fork. https://github.com/verronpro/docx-stamper/