使用fr.opensagres.xdocreport转ODT为PDF时变量为空的问题
Let’s walk through actionable steps to diagnose why your variables are showing up empty in the generated PDF:
1. Fix Accessibility of Your Private Inner POJO Class
Your HerbarySheetComplete is a private non-static inner class, which can cause reflection issues for Freemarker (the template engine you’re using). Freemarker relies on reflection to access getter methods, and private inner classes have restricted visibility that might block this access.
Try adjusting the class to either:
- Make it a public static inner class (if it needs to stay nested in the parent class), or
- Move it to its own top-level public class
Example adjustment:
// Update from private non-static to public static public static class HerbarySheetComplete { // ... existing fields, getters, and setters ... }
2. Verify XDocReport Detects Your Template Variables
Add a debug check after loading the report to confirm XDocReport has parsed your template variables correctly. This helps rule out issues with how the ODT template is formatted:
IXDocReport report = XDocReportRegistry.getRegistry().loadReport(in, TemplateEngineKind.Freemarker); // Print all detected fields to debug IFieldsMetadata fieldsMetadata = report.getFieldsMetadata(); for (IFieldMetadata field : fieldsMetadata.getFields()) { System.out.println("Detected template field: " + field.getName()); }
If your ${h.herbary!} or other variables don’t appear in the output, the problem lies with the ODT template’s markup.
3. Validate ODT Template Markup Integrity
ODT editors sometimes split template tags across XML elements (e.g., if you applied formatting to the text). To check:
- Rename your ODT file to
.zipand extract its contents - Open
content.xmlin a text editor - Search for your variables (like
${h.herbary!}) and ensure they appear as a single continuous text node, not split across<text:span>or other XML tags.
If they’re split, retype the variable tags in the ODT editor using plain text mode (no formatting applied).
4. Enable Debug Logging for More Context
The single INFO log line you’re seeing doesn’t provide enough detail. Adjust your logging configuration to enable DEBUG level for these packages:
fr.opensagres.xdocreport(XDocReport core)org.apache.freemarker(the template engine)
This will show you exactly how Freemarker is resolving variables, including warnings about missing variables or reflection access failures.
5. Simplify the Test Case
Isolate the problem with a minimal test:
- Create a simple public POJO with one field (e.g.,
String testFieldand its getter/setter) - Make an ODT template with
${h.testField!} - Run your conversion code with this setup
If this works, the issue is specific to your original POJO or template. If it still fails, double-check your XDocReport dependency setup (ensure all required jars are included and there are no version conflicts).
内容的提问来源于stack exchange,提问作者user5354385

