XDocReport 将 odt 转换为 pdf 如何设置正确的语言环境



我正在尝试使用 IXDocReport 将一些*.odt文件转换为*.pdf

以下是*.odt文件的假设内容:${amount?string.currency} to be paid

这是我进行转换的代码(您可以在 kotlin REPL 中运行它(:

import fr.opensagres.xdocreport.converter.ConverterTypeTo
import fr.opensagres.xdocreport.converter.ConverterTypeVia
import fr.opensagres.xdocreport.converter.Options
import fr.opensagres.xdocreport.document.IXDocReport
import fr.opensagres.xdocreport.document.registry.XDocReportRegistry
import fr.opensagres.xdocreport.template.TemplateEngineKind
import java.io.ByteArrayInputStream
import java.io.File
val options: Options = Options.getTo(ConverterTypeTo.PDF).via(ConverterTypeVia.ODFDOM)
val content: ByteArray = File("/home/sandro/tmp/report.odt").readBytes()
val templateId: String = "someId"
val registry: XDocReportRegistry = XDocReportRegistry.getRegistry()
val data: MutableMap<String, Any> = mutableMapOf("amount" to 10)
ByteArrayInputStream(content).use { input ->
val report: IXDocReport =
registry.loadReport(input, templateId, TemplateEngineKind.Freemarker, true)
val tmpFile: File = createTempFile("out", ".pdf")
tmpFile.outputStream().use { output ->
report.convert(data, options, output)
println(tmpFile.toString())
}
}

结果是带有字符串$10.00 to be paid的 pdf 文件

如何在转换过程中将所需的区域设置设置为 XDocReport,以便将结果正确更改为其他货币?

附言我无法控制模板本身 - 所以请不要告诉我在模板本身中添加<#setting locale="${bean.locale}">或其他东西。我唯一可以更改的地方是代码。提前谢谢。

附言我需要为每个请求呈现许多模板,并且需要为每个模板设置语言环境。

我从未使用过XDocReport,但也许这会起作用:https://github.com/opensagres/xdocreport/wiki/FreemarkerTemplate"如何配置Freemarker?

从那里引用:

要使用 XDocReport 配置 Freemarker,您必须获取配置实例。要做你必须>

  1. 创建一个类(例如: fr.opensagres.xdocreport.MyFreemarkerConfiguration(,实现 fr.opensagres.xdocreport.document.discovery.ITemplateEngineInitializerDiscovery.
  2. 通过创建文件向 SPI 此类注册 META-INF/services/fr.opensagres.xdocreport.document.discovery.ITemplateEngineInitializerDiscovery 用你的班级的名字: fr.opensagres.xdocreport.MyFreemarkerConfiguration 此文件应为 在您的类路径中(例如,您可以将其托管在 src/META-INF/services/(。

所以你需要这样的类:

public class MyFreemarkerConfiguration implements ITemplateEngineInitializerDiscovery {
[...]
public void initialize(ITemplateEngine templateEngine) {
if (TemplateEngineKind.Freemarker.name().equals( templateEngine.getKind())) {
Configuration cfg = ((FreemarkerTemplateEngine) templateEngine).getFreemarkerConfiguration();
cfg.setLocale(...);
}
}
}

最新更新