XDocReport converting odt to pdf how to set proper locale

1.3k Views Asked by At

I'm trying to convert some *.odt file to *.pdf using IXDocReport.

Here is the hypothetical content of the *.odt file: ${amount?string.currency} to be paid

Here is the code I do conversion with (you can run it in 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())
    }
}

and the result is the pdf file with string $10.00 to be paid

How can I set needed locale to XDocReport during conversion so the result could be changed to other currencies correctly?

P.S. I cannot control the template itself - so please do not tell me to add <#setting locale="${bean.locale}"> or something else to the template itself. The only place I can change is the code. Thanks in advance.

P.P.S. I need to render many templates per request and need to set locale per each template.

1

There are 1 best solutions below

4
ddekany On

I have never used XDocReport, but maybe this will work: https://github.com/opensagres/xdocreport/wiki/FreemarkerTemplate "How to configure Freemarker?"

Quotation from there:

To configure Freemarker with XDocReport you must get the Configuration instance. To do > that you must

  1. create a class (ex : fr.opensagres.xdocreport.MyFreemarkerConfiguration) which implements fr.opensagres.xdocreport.document.discovery.ITemplateEngineInitializerDiscovery.
  2. register with SPI this class by creating the file META-INF/services/fr.opensagres.xdocreport.document.discovery.ITemplateEngineInitializerDiscovery with the name of you class : fr.opensagres.xdocreport.MyFreemarkerConfiguration This file should be in your classpath (you can for instance host it in the src/META-INF/services/ of your project).

So you will need a class like this:

public class MyFreemarkerConfiguration implements ITemplateEngineInitializerDiscovery {

    [...]

    public void initialize(ITemplateEngine templateEngine) {
        if (TemplateEngineKind.Freemarker.name().equals( templateEngine.getKind())) {
            Configuration cfg = ((FreemarkerTemplateEngine) templateEngine).getFreemarkerConfiguration();
            cfg.setLocale(...);
        }
    }

}