0

*.odt一部のファイルをIXDocReport*.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())
    }
}

結果は文字列を含む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

そこからの引用:

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 に答える