得到这个异常:

fr.opensagres.xdocreport.core.XDocReportException: java.lang.NoSuchMethodError: freemarker.template.Configuration.setTagSyntax(I)V
  at fr.opensagres.xdocreport.template.AbstractTemplateEngine.process(AbstractTemplateEngine.java:151)
  at fr.opensagres.xdocreport.template.AbstractTemplateEngine.process(AbstractTemplateEngine.java:83)
  at fr.opensagres.xdocreport.document.AbstractXDocReport.processTemplateEngine(AbstractXDocReport.java:775)
  at fr.opensagres.xdocreport.document.AbstractXDocReport.convert(AbstractXDocReport.java:703)
  at com.altruista.mp.dao.ContactSyncImpl$1.processRow(ContactSyncImpl.java:231)
  at org.springframework.jdbc.core.JdbcTemplate$RowCallbackHandlerResultSetExtractor.extractData(JdbcTemplate.java:1607)
  at org.springframework.jdbc.core.JdbcTemplate$1.doInPreparedStatement(JdbcTemplate.java:708)
  at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:644)
  at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:695)
  at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:727)
  at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:752)
  at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:762)
  at com.altruista.mp.dao.ContactSyncImpl.applyRemoteChanges(ContactSyncImpl.java:128)
  at com.altruista.mp.sync.SyncController.run(SyncController.java:137)
  at org.springframework.boot.SpringApplication.runCommandLineRunners(SpringApplication.java:634)
  at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:652)
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:312)
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:909)
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:898)
  at com.altruista.mp.sync.Application.main(Application.java:19)
Caused by: java.lang.NoSuchMethodError: freemarker.template.Configuration.setTagSyntax(I)V
  at fr.opensagres.xdocreport.template.freemarker.FreemarkerTemplateEngine.setFreemarkerConfiguration(FreemarkerTemplateEngine.java:184)
  at fr.opensagres.xdocreport.template.freemarker.FreemarkerTemplateEngine.getDefaultConfiguration(FreemarkerTemplateEngine.java:256)
  at fr.opensagres.xdocreport.template.freemarker.FreemarkerTemplateEngine.getFreemarkerConfiguration(FreemarkerTemplateEngine.java:174)
  at fr.opensagres.xdocreport.template.freemarker.FreemarkerTemplateEngine.processWithCache(FreemarkerTemplateEngine.java:110)
  at fr.opensagres.xdocreport.template.AbstractTemplateEngine.process(AbstractTemplateEngine.java:111)
  ... 19 more
CONTACT: Couchbase [7207d5b6-cea2-48ee-8464-bc73ce21aa5f] <= SQL [ 582375 ]
java.io.FileNotFoundException: Template fr.opensagres.xdocreport.document.odt.ODTReport@fbe57c!content.xml not found.
  at freemarker.template.Configuration.getTemplate(Configuration.java:441)
  at freemarker.template.Configuration.getTemplate(Configuration.java:404)
  at fr.opensagres.xdocreport.template.freemarker.FreemarkerTemplateEngine.processWithCache(FreemarkerTemplateEngine.java:110)
  at fr.opensagres.xdocreport.template.AbstractTemplateEngine.process(AbstractTemplateEngine.java:111)
  at fr.opensagres.xdocreport.template.AbstractTemplateEngine.process(AbstractTemplateEngine.java:83)
  at fr.opensagres.xdocreport.document.AbstractXDocReport.processTemplateEngine(AbstractXDocReport.java:775)
  at fr.opensagres.xdocreport.document.AbstractXDocReport.convert(AbstractXDocReport.java:703)
  at com.altruista.mp.dao.ContactSyncImpl$1.processRow(ContactSyncImpl.java:231)
  at org.springframework.jdbc.core.JdbcTemplate$RowCallbackHandlerResultSetExtractor.extractData(JdbcTemplate.java:1607)
  at org.springframework.jdbc.core.JdbcTemplate$1.doInPreparedStatement(JdbcTemplate.java:708)
  at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:644)
  at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:695)
  at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:727)
  at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:752)
  at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:762)
  at com.altruista.mp.dao.ContactSyncImpl.applyRemoteChanges(ContactSyncImpl.java:128)
  at com.altruista.mp.sync.SyncController.run(SyncController.java:137)
  at org.springframework.boot.SpringApplication.runCommandLineRunners(SpringApplication.java:634)
  at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:652)
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:312)
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:909)
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:898)
  at com.altruista.mp.sync.Application.main(Application.java:19)


使用以下依赖:

<dependency>
  <groupId>commons-fileupload</groupId>
  <artifactId>commons-fileupload</artifactId>
  <version>1.2.1</version>
  <optional>true</optional>
</dependency>
<dependency>
    <groupId>fr.opensagres.xdocreport</groupId>
    <artifactId>fr.opensagres.xdocreport.template.freemarker</artifactId>
    <version>1.0.4</version>
    </dependency>
<dependency>
  <groupId>fr.opensagres.xdocreport</groupId>
  <artifactId>fr.opensagres.xdocreport.core</artifactId>
  <version>1.0.4</version>
</dependency>
<dependency>
  <groupId>fr.opensagres.xdocreport</groupId>
  <artifactId>fr.opensagres.xdocreport.converter</artifactId>
  <version>1.0.4</version>
</dependency>
<dependency>
  <groupId>fr.opensagres.xdocreport</groupId>
  <artifactId>fr.opensagres.xdocreport.template</artifactId>
  <version>1.0.4</version>
</dependency>
<dependency>
  <groupId>fr.opensagres.xdocreport</groupId>
  <artifactId>fr.opensagres.xdocreport.document</artifactId>
  <version>1.0.4</version>
</dependency>
<dependency>
  <groupId>fr.opensagres.xdocreport</groupId>
  <artifactId>fr.opensagres.xdocreport.document.docx</artifactId>
  <version>1.0.4</version>
    </dependency>
    <dependency>
    <groupId>fr.opensagres.xdocreport</groupId>
    <artifactId>fr.opensagres.xdocreport.document.odt</artifactId>
    <version>1.0.4</version>
   </dependency>
   <dependency>
        <groupId>fr.opensagres.xdocreport</groupId>
        <artifactId>fr.opensagres.xdocreport.converter.odt.odfdom</artifactId>
        <version>1.0.4</version>
   </dependency>
<dependency>
  <groupId>xerces</groupId>
  <artifactId>xercesImpl</artifactId>
  <version>2.9.1</version>
  <optional>true</optional>
</dependency>


带代码

InputStream in= new FileInputStream(new File("src/main/resources/demoOffice.odt"));
IXDocReport report = XDocReportRegistry.getRegistry().loadReport(
    in, TemplateEngineKind.Freemarker);

// 2) Create Java model context
IContext context = report.createContext();
context.put("name", "world");

// 3) Set PDF as format converter
Options options = Options.getTo(ConverterTypeTo.PDF);

// 4) Generate report by merging Java model with the ODT and convert it to PDF
OutputStream outputStream = new FileOutputStream(new File("src/main/resources/demoOffice_Out.odt"));
report.convert(context, options, outputStream);


在demoOffice.odt中:

hello ${name}!

最佳答案

XDocReport是模块化的,因此您必须选择:


您的模板引擎实现。在您的情况下,您希望使用Freemarker,因此必须在pom中声明:



<dependency>
  <groupId>fr.opensagres.xdocreport</groupId>
  <artifactId>fr.opensagres.xdocreport.template.freemarker</artifactId>
  <version>1.0.4</version>
</dependency>




文档种类(doc,odt等)。在您的情况下,您希望使用odt,因此必须在pom中声明:



<dependency>
  <groupId>fr.opensagres.xdocreport</groupId>
  <artifactId>fr.opensagres.xdocreport.document.odt</artifactId>
  <version>1.0.4</version>
</dependency>




如果您希望将报告转换为原始格式以外的格式,请使用转换器。在您的情况下,您希望将ODT转换为PDF,因此必须在pom中声明:



<dependency>
  <groupId>fr.opensagres.xdocreport</groupId>
  <artifactId>fr.opensagres.xdocreport.converter.odt.odfdom</artifactId>
  <version>1.0.4</version>
</dependency>



这些依赖项在here中进行了解释

关于java - 使用freemarker加载odt文件(文档模板)。如何将odt文件生成为pdf,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26047366/

10-10 16:39