You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用fr.opensagres.xdocreport转ODT为PDF时变量为空的问题

Troubleshooting Empty Variables in ODT-to-PDF Conversion with XDocReport

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:

  1. Rename your ODT file to .zip and extract its contents
  2. Open content.xml in a text editor
  3. 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 testField and 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:45:31