0

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

这是*.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())
    }
}

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

如何在转换期间将所需的语言环境设置为 XDocReport,以便可以正确地将结果更改为其他货币?

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

PPS我需要为每个请求渲染许多模板,并且需要为每个模板设置语言环境。

4

1 回答 1

0

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

那里的报价:

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

  1. 创建一个实现 fr.opensagres.xdocreport.document.discovery.ITemplateEngineInitializerDiscovery 的类(例如:fr.opensagres.xdocreport.MyFreemarkerConfiguration)。
  2. 通过使用您的类的名称创建文件 META-INF/services/fr.opensagres.xdocreport.document.discovery.ITemplateEngineInitializerDiscovery 来向 SPI 注册此类: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(...);
        }
    }

}
于 2019-11-30T21:34:22.497 回答