使用freemarker加载odt文件(文档模板)。如何生成odt文件到PDF格式

时间:2014-09-25 20:17:52

标签: java spring-mvc freemarker xdocreport xdoc

获得此例外:

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}!

1 个答案:

答案 0 :(得分:3)

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