java Sping Boot 应用中的BIRT

xzlaal3s  于 2023-06-04  发布在  Java
关注(0)|答案(3)|浏览(632)

我需要在现有的Sping Boot Web应用程序中创建一个报告功能(函数)。我的建议是使用BIRT,我可以将其与Sping Boot Web应用程序集成。
我找到了下面的文章,并能够在spring Boot starter项目中运行报告(使用http://start.spring.io/)。这篇文章,相当旧,确实帮助我找到了一个工作示例。https://spring.io/blog/2012/01/30/spring-framework-birt .这篇文章基本上正是我想要的,但在一个Spring Boot Web应用程序。
我面临的挑战是使用BIRT查看器运行报告,该查看器具有很好的附加功能。(打印、Expoet数据、PDF、分页等)
我找不到任何像本文描述的那样使用BIRT的Sping Boot 示例。
我的问题是:
1.是否有替代或其他方式在Sping Boot Web应用程序中执行报告?(显然不想从头开始创建类似BIRT的功能,或者尽可能地将报表引擎与Web应用程序分开运行,从而重新发明轮子)
1.今天有没有人在Sping Boot Web应用程序中使用BIRT(与查看器一起),并愿意分享或教育我如何最好地做到这一点?(我试图让JSP页面与Sping Boot 一起工作,但无法如此成功......更多的是缺乏经验)
有人能帮帮我吗。
亲切的问候,亨克

rbpvctlc

rbpvctlc1#

依赖项、IReportEngine的示例和目录是在Sping Boot Web应用程序中设置BIRT最具挑战性的部分。这个例子有一点错误处理,并且依赖于环境变量来指定重要目录的路径。
这个设置的一个优点是,它是一个独立的API,用于构建和生成报告。作为一个附带的好处,它根本不依赖于JSP。
在Docker容器中运行。
最初有一个计划,使用一些接口来尝试其他报告框架,如碧玉报告或其他东西。
这并不是你所要求的,尽管在我看来,在某些情况下这是更好的。在将BIRT报告运行器作为这样的应用程序使用、配置和部署时,您有很大的灵活性。这不使用Actian提供的预打包BIRT查看器。
我将为报告创建一个名为birt-report-runner-data的数据容器,然后将所有用于日志记录和报告的目录放在该容器上,然后您可以将其挂载到BIRT容器上。

主要组件

主文件(BirtReportRunnerApplication.java)

  1. package com.example;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class BirtReportRunnerApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(BirtReportRunnerApplication.class, args);
  8. }
  9. }

接收报表请求的控制器(ReportController.java)

  1. package com.example.core.web.controller;
  2. import com.example.core.model.BIRTReport;
  3. import com.example.core.service.ReportRunner;
  4. import com.example.core.web.dto.ReportRequest;
  5. import org.slf4j.Logger;
  6. import org.slf4j.LoggerFactory;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.beans.factory.annotation.Qualifier;
  9. import org.springframework.http.HttpHeaders;
  10. import org.springframework.http.HttpStatus;
  11. import org.springframework.http.MediaType;
  12. import org.springframework.http.ResponseEntity;
  13. import org.springframework.web.bind.annotation.RequestBody;
  14. import org.springframework.web.bind.annotation.RequestMapping;
  15. import org.springframework.web.bind.annotation.RequestMethod;
  16. import org.springframework.web.bind.annotation.RestController;
  17. @RestController("ReportController")
  18. @RequestMapping("/reports")
  19. public class ReportController {
  20. private Logger logger = LoggerFactory.getLogger(ReportController.class);
  21. @Autowired
  22. @Qualifier("birt")
  23. ReportRunner reportRunner;
  24. @RequestMapping(value = "/birt", method = RequestMethod.POST)
  25. public ResponseEntity<byte[]> getBIRTReport(@RequestBody ReportRequest reportRequest) {
  26. byte[] reportBytes;
  27. ResponseEntity<byte[]> responseEntity;
  28. try {
  29. logger.info("REPORT REQUEST NAME: " + reportRequest.getReportName());
  30. reportBytes =
  31. new BIRTReport(
  32. reportRequest.getReportName(),
  33. reportRequest.getReportParameters(),
  34. reportRunner)
  35. .runReport().getReportContent().toByteArray();
  36. HttpHeaders httpHeaders = new HttpHeaders();
  37. httpHeaders.setContentType(MediaType.parseMediaType("application/pdf"));
  38. String fileName = reportRequest.getReportName() + ".pdf";
  39. httpHeaders.setContentDispositionFormData(fileName, fileName);
  40. httpHeaders.setCacheControl("must-revalidate, post-check=0, pre-check=0");
  41. responseEntity = new ResponseEntity<byte[]>(reportBytes, httpHeaders, HttpStatus.OK);
  42. } catch (Exception e) {
  43. responseEntity = new ResponseEntity<byte[]>(HttpStatus.NOT_IMPLEMENTED);
  44. return responseEntity;
  45. }
  46. return responseEntity;
  47. }
  48. }

指定运行报表的报表请求DTO(ReportRequest.java)

  1. package com.example.core.web.dto;
  2. import com.fasterxml.jackson.annotation.JsonProperty;
  3. import java.util.List;
  4. public class ReportRequest {
  5. private String reportName;
  6. private String reportParameters;
  7. public ReportRequest(@JsonProperty("reportName") String reportName,
  8. @JsonProperty("reportParameters") String reportParameters) {
  9. this.reportName = reportName;
  10. this.reportParameters = reportParameters;
  11. }
  12. public String getReportName() {
  13. return reportName;
  14. }
  15. public String getReportParameters() {
  16. return reportParameters;
  17. }
  18. public void setReportParameters(String reportParameters) {
  19. this.reportParameters = reportParameters;
  20. }
  21. }

报表运行器接口(ReportRunner.java)

  1. package com.example.core.service;
  2. import com.example.core.model.Report;
  3. import java.io.ByteArrayOutputStream;
  4. public interface ReportRunner {
  5. ByteArrayOutputStream runReport(Report report);
  6. }

完成大部分工作的最大类(BIRTReportRunner.java)

  1. package com.example.core.service;
  2. import com.example.core.model.Report;
  3. import org.eclipse.birt.core.exception.BirtException;
  4. import org.eclipse.birt.core.framework.Platform;
  5. import org.eclipse.birt.report.engine.api.*;
  6. import org.eclipse.core.internal.registry.RegistryProviderFactory;
  7. import org.slf4j.Logger;
  8. import org.slf4j.LoggerFactory;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.beans.factory.annotation.Qualifier;
  11. import org.springframework.core.env.Environment;
  12. import org.springframework.stereotype.Service;
  13. import javax.annotation.PostConstruct;
  14. import javax.annotation.PreDestroy;
  15. import java.io.ByteArrayOutputStream;
  16. import java.io.File;
  17. import java.nio.file.Files;
  18. import java.nio.file.Path;
  19. import java.nio.file.Paths;
  20. import java.util.HashMap;
  21. import java.util.Map;
  22. import java.util.logging.Level;
  23. @Service
  24. @Qualifier("birt")
  25. public class BIRTReportRunner implements ReportRunner {
  26. private static final String DEFAULT_LOGGING_DIRECTORY = "defaultBirtLoggingDirectory/";
  27. private Logger logger = LoggerFactory.getLogger(BIRTReportRunner.class);
  28. private static String reportOutputDirectory;
  29. private IReportEngine birtReportEngine = null;
  30. @Autowired
  31. private Environment env;
  32. /**
  33. * Starts up and configures the BIRT Report Engine
  34. */
  35. @PostConstruct
  36. public void startUp() {
  37. if(env.getProperty("birt_report_input_dir") == null)
  38. throw new RuntimeException("Cannot start application since birt report input directory was not specified.");
  39. try {
  40. String birtLoggingDirectory = env.getProperty("birt_logging_directory") == null ? DEFAULT_LOGGING_DIRECTORY : env.getProperty("birt_logging_directory");
  41. Level birtLoggingLevel = env.getProperty("birt_logging_level") == null ? Level.SEVERE : Level.parse(env.getProperty("birt_logging_level"));
  42. EngineConfig engineConfig = new EngineConfig();
  43. logger.info("BIRT LOG DIRECTORY SET TO : {}", birtLoggingDirectory);
  44. logger.info("BIRT LOGGING LEVEL SET TO {}", birtLoggingLevel);
  45. engineConfig.setLogConfig(birtLoggingDirectory, birtLoggingLevel);
  46. // Required due to a bug in BIRT that occurs in calling Startup after the Platform has already been started up
  47. RegistryProviderFactory.releaseDefault();
  48. Platform.startup(engineConfig);
  49. IReportEngineFactory reportEngineFactory = (IReportEngineFactory) Platform.createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
  50. birtReportEngine = reportEngineFactory.createReportEngine(engineConfig);
  51. } catch (BirtException e) {
  52. // TODO add logging aspect and find out how to log a platform startup problem from this catch block, if possible, using the aspect.
  53. // Possibly rethrow the exception here and catch it in the aspect.
  54. logger.error("Birt Startup Error: {}", e.getMessage());
  55. }
  56. reportOutputDirectory = env.getProperty("birt_temp_file_output_dir");
  57. }
  58. /**
  59. * Shuts down the BIRT Report Engine
  60. */
  61. @PreDestroy
  62. public void shutdown() {
  63. birtReportEngine.destroy();
  64. RegistryProviderFactory.releaseDefault();
  65. Platform.shutdown();
  66. }
  67. public File getReportFromFilesystem(String reportName) throws RuntimeException {
  68. String reportDirectory = env.getProperty("birt_report_input_dir");
  69. Path birtReport = Paths.get(reportDirectory + File.separator + reportName + ".rptdesign");
  70. if(!Files.isReadable(birtReport))
  71. throw new RuntimeException("Report " + reportName + " either did not exist or was not writable.");
  72. return birtReport.toFile();
  73. }
  74. /**
  75. * This method creates and executes the report task, the main responsibility
  76. * of the entire Report Service.
  77. * This method is key to enabling pagination for the BIRT report. The IRunTask run task
  78. * is created and then used to generate an ".rptdocument" binary file.
  79. * This binary file is then read by the separately created IRenderTask render
  80. * task. The render task renders the binary document as a binary PDF output
  81. * stream which is then returned from the method.
  82. * <p>
  83. *
  84. * @param birtReport the report object created at the controller to hold the data of the report request.
  85. * @return Returns a ByteArrayOutputStream of the PDF bytes generated by the
  86. */
  87. @Override
  88. public ByteArrayOutputStream runReport(Report birtReport) {
  89. ByteArrayOutputStream byteArrayOutputStream;
  90. File rptDesignFile;
  91. // get the path to the report design file
  92. try {
  93. rptDesignFile = getReportFromFilesystem(birtReport.getName());
  94. } catch (Exception e) {
  95. logger.error("Error while loading rptdesign: {}.", e.getMessage());
  96. throw new RuntimeException("Could not find report");
  97. }
  98. // process any additional parameters
  99. Map<String, String> parsedParameters = parseParametersAsMap(birtReport.getParameters());
  100. byteArrayOutputStream = new ByteArrayOutputStream();
  101. try {
  102. IReportRunnable reportDesign = birtReportEngine.openReportDesign(rptDesignFile.getPath());
  103. IRunTask runTask = birtReportEngine.createRunTask(reportDesign);
  104. if (parsedParameters.size() > 0) {
  105. for (Map.Entry<String, String> entry : parsedParameters.entrySet()) {
  106. runTask.setParameterValue(entry.getKey(), entry.getValue());
  107. }
  108. }
  109. runTask.validateParameters();
  110. String rptdocument = reportOutputDirectory + File.separator
  111. + "generated" + File.separator
  112. + birtReport.getName() + ".rptdocument";
  113. runTask.run(rptdocument);
  114. IReportDocument reportDocument = birtReportEngine.openReportDocument(rptdocument);
  115. IRenderTask renderTask = birtReportEngine.createRenderTask(reportDocument);
  116. PDFRenderOption pdfRenderOption = new PDFRenderOption();
  117. pdfRenderOption.setOption(IPDFRenderOption.REPAGINATE_FOR_PDF, new Boolean(true));
  118. pdfRenderOption.setOutputFormat("pdf");
  119. pdfRenderOption.setOutputStream(byteArrayOutputStream);
  120. renderTask.setRenderOption(pdfRenderOption);
  121. renderTask.render();
  122. renderTask.close();
  123. } catch (EngineException e) {
  124. logger.error("Error while running report task: {}.", e.getMessage());
  125. // TODO add custom message to thrown exception
  126. throw new RuntimeException();
  127. }
  128. return byteArrayOutputStream;
  129. }
  130. /**
  131. * Takes a String of parameters started by '?', delimited by '&', and with
  132. * keys and values split by '=' and returnes a Map of the keys and values
  133. * in the String.
  134. *
  135. * @param reportParameters a String from a HTTP request URL
  136. * @return a map of parameters with Key,Value entries as strings
  137. */
  138. public Map<String, String> parseParametersAsMap(String reportParameters) {
  139. Map<String, String> parsedParameters = new HashMap<String, String>();
  140. String[] paramArray;
  141. if (reportParameters.isEmpty()) {
  142. throw new IllegalArgumentException("Report parameters cannot be empty");
  143. } else if (!reportParameters.startsWith("?") && !reportParameters.contains("?")) {
  144. throw new IllegalArgumentException("Report parameters must start with a question mark '?'!");
  145. } else {
  146. String noQuestionMark = reportParameters.substring(1, reportParameters.length());
  147. paramArray = noQuestionMark.split("&");
  148. for (String param : paramArray) {
  149. String[] paramGroup = param.split("=");
  150. if (paramGroup.length == 2) {
  151. parsedParameters.put(paramGroup[0], paramGroup[1]);
  152. } else {
  153. parsedParameters.put(paramGroup[0], "");
  154. }
  155. }
  156. }
  157. return parsedParameters;
  158. }
  159. }

报表对象类(Report.java)

  1. package com.example.core.model;
  2. import com.example.core.service.ReportRunner;
  3. import java.io.ByteArrayOutputStream;
  4. import java.util.List;
  5. /**
  6. * A Report object has a byte representation of the report output that can be
  7. * used to write to any output stream. This class is designed around the concept
  8. * of using ByteArrayOutputStreams to write PDFs to an output stream.
  9. *
  10. *
  11. */
  12. public abstract class Report {
  13. protected String name;
  14. protected String parameters;
  15. protected ByteArrayOutputStream reportContent;
  16. protected ReportRunner reportRunner;
  17. public Report(String name, String parameters, ReportRunner reportRunner) {
  18. this.name = name;
  19. this.parameters = parameters;
  20. this.reportRunner = reportRunner;
  21. }
  22. /**
  23. * This is the processing method for a Report. Once the report is ran it
  24. * populates an internal field with a ByteArrayOutputStream of the
  25. * report content generated during the run process.
  26. * @return Returns itself with the report content output stream created.
  27. */
  28. public abstract Report runReport();
  29. public ByteArrayOutputStream getReportContent() {
  30. return this.reportContent;
  31. }
  32. public String getName() {
  33. return name;
  34. }
  35. public String getParameters() {
  36. return parameters;
  37. }
  38. }

BIRTReport对象类(BIRTReport.java)

  1. package com.example.core.model;
  2. import com.example.core.service.ReportRunner;
  3. public class BIRTReport extends Report {
  4. public BIRTReport(String name, String reportParameters, ReportRunner reportRunner) {
  5. super(name, reportParameters, reportRunner);
  6. }
  7. @Override
  8. public Report runReport() {
  9. this.reportContent = reportRunner.runReport(this);
  10. return this;
  11. }
  12. }

构建文件(build.gradle)

  1. buildscript {
  2. ext {
  3. springBootVersion = '1.3.2.RELEASE'
  4. }
  5. repositories {
  6. jcenter()
  7. }
  8. dependencies {
  9. classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
  10. classpath("io.spring.gradle:dependency-management-plugin:0.5.2.RELEASE")
  11. }
  12. }
  13. apply plugin: 'java'
  14. apply plugin: 'eclipse'
  15. apply plugin: 'idea'
  16. apply plugin: 'spring-boot'
  17. jar {
  18. baseName = 'com.example.simple-birt-runner'
  19. version = '1.0.0'
  20. }
  21. sourceCompatibility = 1.8
  22. targetCompatibility = 1.8
  23. ext {
  24. springBootVersion = '1.3.0.M5'
  25. }
  26. repositories {
  27. jcenter()
  28. mavenCentral()
  29. }
  30. dependencies {
  31. compile("org.springframework.boot:spring-boot-starter-actuator:${springBootVersion}")
  32. compile("org.springframework.boot:spring-boot-starter-web:${springBootVersion}")
  33. // BIRT Runtime and ReportEngine dependencies
  34. // These were pulled from the Actuate download site at http://download.eclipse.org/birt/downloads/ for version 4.5.0
  35. compile fileTree(dir: 'lib', include: '*.jar')
  36. /*compile("org.eclipse.birt.runtime:org.eclipse.birt.runtime:4.4.2") {
  37. exclude group: "org.eclipse.birt.runtime", module: "org.apache.poi"
  38. exclude group: "org.eclipse.birt.runtime", module: "org.eclipse.orbit.mongodb"
  39. }*/
  40. compile("org.springframework.boot:spring-boot-starter-tomcat")
  41. // include any runtime JDBC driver dependencies here
  42. testCompile("org.springframework.boot:spring-boot-starter-test")
  43. }

依赖

获取Birt Viewer lib目录中的所有依赖项,并将它们放在类路径中。以下是外部依赖项的完整列表:

  1. com.ibm.icu_54.1.1.v201501272100.jar
  2. com.lowagie.text_2.1.7.v201004222200.jar
  3. derby.jar
  4. flute.jar
  5. javax.wsdl_1.5.1.v201012040544.jar
  6. javax.xml.stream_1.0.1.v201004272200.jar
  7. javax.xml_1.3.4.v201005080400.jar
  8. net.sourceforge.lpg.lpgjavaruntime_1.1.0.v201004271650.jar
  9. org.apache.batik.bridge_1.6.0.v201011041432.jar
  10. org.apache.batik.css_1.6.0.v201011041432.jar
  11. org.apache.batik.dom.svg_1.6.0.v201011041432.jar
  12. org.apache.batik.dom_1.6.1.v201505192100.jar
  13. org.apache.batik.ext.awt_1.6.0.v201011041432.jar
  14. org.apache.batik.parser_1.6.0.v201011041432.jar
  15. org.apache.batik.pdf_1.6.0.v201105071520.jar
  16. org.apache.batik.svggen_1.6.0.v201011041432.jar
  17. org.apache.batik.transcoder_1.6.0.v201011041432.jar
  18. org.apache.batik.util.gui_1.6.0.v201011041432.jar
  19. org.apache.batik.util_1.6.0.v201011041432.jar
  20. org.apache.batik.xml_1.6.0.v201011041432.jar
  21. org.apache.commons.codec_1.6.0.v201305230611.jar
  22. org.apache.commons.logging_1.1.1.v201101211721.jar
  23. org.apache.lucene.core_3.5.0.v20120725-1805.jar
  24. org.apache.poi_3.9.0.v201405241750.jar
  25. org.apache.xerces_2.9.0.v201101211617.jar
  26. org.apache.xml.resolver_1.2.0.v201005080400.jar
  27. org.apache.xml.serializer_2.7.1.v201005080400.jar
  28. org.eclipse.birt.runtime_4.5.0.jar
  29. org.eclipse.core.contenttype_3.5.0.v20150421-2214.jar
  30. org.eclipse.core.expressions_3.5.0.v20150421-2214.jar
  31. org.eclipse.core.filesystem_1.5.0.v20150421-0713.jar
  32. org.eclipse.core.jobs_3.7.0.v20150330-2103.jar
  33. org.eclipse.core.resources_3.10.0.v20150423-0755.jar
  34. org.eclipse.core.runtime.compatibility_3.2.300.v20150423-0821.jar
  35. org.eclipse.core.runtime_3.11.0.v20150405-1723.jar
  36. org.eclipse.datatools.connectivity.apache.derby.dbdefinition_1.0.2.v201107221459.jar
  37. org.eclipse.datatools.connectivity.apache.derby_1.0.103.v201212070447.jar
  38. org.eclipse.datatools.connectivity.console.profile_1.0.10.v201109250955.jar
  39. org.eclipse.datatools.connectivity.db.generic_1.0.1.v201107221459.jar
  40. org.eclipse.datatools.connectivity.dbdefinition.genericJDBC_1.0.2.v201310181001.jar
  41. org.eclipse.datatools.connectivity.oda.consumer_3.2.6.v201403131814.jar
  42. org.eclipse.datatools.connectivity.oda.design_3.3.6.v201403131814.jar
  43. org.eclipse.datatools.connectivity.oda.flatfile_3.1.8.v201403010906.jar
  44. org.eclipse.datatools.connectivity.oda.profile_3.2.9.v201403131814.jar
  45. org.eclipse.datatools.connectivity.oda_3.4.3.v201405301249.jar
  46. org.eclipse.datatools.connectivity.sqm.core_1.2.8.v201401230755.jar
  47. org.eclipse.datatools.connectivity_1.2.11.v201401230755.jar
  48. org.eclipse.datatools.enablement.hsqldb.dbdefinition_1.0.0.v201107221502.jar
  49. org.eclipse.datatools.enablement.hsqldb_1.0.0.v201107221502.jar
  50. org.eclipse.datatools.enablement.ibm.db2.iseries.dbdefinition_1.0.3.v201107221502.jar
  51. org.eclipse.datatools.enablement.ibm.db2.iseries_1.0.2.v201107221502.jar
  52. org.eclipse.datatools.enablement.ibm.db2.luw.dbdefinition_1.0.7.v201405302027.jar
  53. org.eclipse.datatools.enablement.ibm.db2.luw_1.0.3.v201401170830.jar
  54. org.eclipse.datatools.enablement.ibm.db2.zseries.dbdefinition_1.0.4.v201107221502.jar
  55. org.eclipse.datatools.enablement.ibm.db2.zseries_1.0.2.v201107221502.jar
  56. org.eclipse.datatools.enablement.ibm.db2_1.0.0.v201401170830.jar
  57. org.eclipse.datatools.enablement.ibm.informix.dbdefinition_1.0.4.v201107221502.jar
  58. org.eclipse.datatools.enablement.ibm.informix_1.0.1.v201107221502.jar
  59. org.eclipse.datatools.enablement.ibm_1.0.0.v201401170830.jar
  60. org.eclipse.datatools.enablement.msft.sqlserver.dbdefinition_1.0.1.v201201240505.jar
  61. org.eclipse.datatools.enablement.msft.sqlserver_1.0.3.v201308161009.jar
  62. org.eclipse.datatools.enablement.mysql.dbdefinition_1.0.4.v201109022331.jar
  63. org.eclipse.datatools.enablement.mysql_1.0.4.v201212120617.jar
  64. org.eclipse.datatools.enablement.oda.ws_1.2.6.v201403131825.jar
  65. org.eclipse.datatools.enablement.oda.xml_1.2.5.v201403131825.jar
  66. org.eclipse.datatools.enablement.oracle.dbdefinition_1.0.103.v201206010214.jar
  67. org.eclipse.datatools.enablement.oracle_1.0.0.v201107221506.jar
  68. org.eclipse.datatools.enablement.postgresql.dbdefinition_1.0.2.v201110070445.jar
  69. org.eclipse.datatools.enablement.postgresql_1.1.1.v201205252207.jar
  70. org.eclipse.datatools.enablement.sap.maxdb.dbdefinition_1.0.0.v201107221507.jar
  71. org.eclipse.datatools.enablement.sap.maxdb_1.0.0.v201107221507.jar
  72. org.eclipse.datatools.modelbase.dbdefinition_1.0.2.v201107221519.jar
  73. org.eclipse.datatools.modelbase.derby_1.0.0.v201107221519.jar
  74. org.eclipse.datatools.modelbase.sql.query_1.1.4.v201212120619.jar
  75. org.eclipse.datatools.modelbase.sql_1.0.6.v201208230744.jar
  76. org.eclipse.datatools.sqltools.data.core_1.2.3.v201212120623.jar
  77. org.eclipse.datatools.sqltools.parsers.sql.lexer_1.0.1.v201107221520.jar
  78. org.eclipse.datatools.sqltools.parsers.sql.query_1.2.1.v201201250511.jar
  79. org.eclipse.datatools.sqltools.parsers.sql_1.0.2.v201107221520.jar
  80. org.eclipse.datatools.sqltools.result_1.1.6.v201402080246.jar
  81. org.eclipse.emf.common_2.11.0.v20150512-0501.jar
  82. org.eclipse.emf.ecore.change_2.11.0.v20150512-0501.jar
  83. org.eclipse.emf.ecore.xmi_2.11.0.v20150512-0501.jar
  84. org.eclipse.emf.ecore_2.11.0.v20150512-0501.jar
  85. org.eclipse.equinox.app_1.3.300.v20150423-1356.jar
  86. org.eclipse.equinox.common_3.7.0.v20150402-1709.jar
  87. org.eclipse.equinox.preferences_3.5.300.v20150408-1437.jar
  88. org.eclipse.equinox.registry_3.6.0.v20150318-1503.jar
  89. org.eclipse.help_3.6.0.v20130326-1254.jar
  90. org.eclipse.osgi.services_3.5.0.v20150519-2006.jar
  91. org.eclipse.osgi_3.10.100.v20150529-1857.jar
  92. org.eclipse.update.configurator_3.3.300.v20140518-1928.jar
  93. org.mozilla.javascript_1.7.5.v201504281450.jar
  94. org.w3c.css.sac_1.3.1.v200903091627.jar
  95. org.w3c.dom.events_3.0.0.draft20060413_v201105210656.jar
  96. org.w3c.dom.smil_1.0.1.v200903091627.jar
  97. org.w3c.dom.svg_1.1.0.v201011041433.jar

应用属性(application.yml)

  1. birt:
  2. report:
  3. output:
  4. dir: ${birt_temp_file_output_dir}
  5. input:
  6. dir: ${birt_report_input_dir}
  7. logging:
  8. level: ${birt_logging_level}
  9. server:
  10. port: 8080
  11. context-path: /simple-birt-runner
  12. birt:
  13. logging:
  14. level: SEVERE
  15. logging:
  16. level:
  17. org:
  18. springframework:
  19. web: ERROR

运行Dockerfile(Dockerfile)

  1. FROM java:openjdk-8u66-jre
  2. MAINTAINER Kent O. Johnson <kentoj@gmail.com>
  3. COPY com.example.simple-birt-runner-*.jar /opt/soft/simple-birt-runner.jar
  4. RUN mkdir -p /reports/input \
  5. && mkdir /reports/output \
  6. && mkdir -p /reports/log/engine
  7. WORKDIR /opt/soft
  8. CMD java -jar -Xms128M -Xmx4G simple-birt-runner.jar
  9. EXPOSE 8080

运行镜像的Docker compose文件(docker-compose.yml)

  1. simple-birt-runner:
  2. image: soft/simple-birt-runner-release
  3. ports:
  4. - "8090:8080"
  5. environment:
  6. - birt_temp_file_output_dir=/reports/output
  7. - birt_report_input_dir=/reports/input
  8. - birt_logging_directory=/reports/log/engine
  9. - birt_logging_level=SEVERE
  10. volumes_from:
  11. - birt-report-runner-data
展开查看全部
nkkqxpd9

nkkqxpd92#

关于@肯特Json的回答。我没有设法用gradle配置项目,但我设法用maven构建了它。下面是pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>birt-runner</groupId>
  7. <artifactId>com.example.simple-birt-runner</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <packaging>jar</packaging>
  10. <parent>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-parent</artifactId>
  13. <version>1.4.4.RELEASE</version>
  14. <relativePath/> <!-- lookup parent from repository -->
  15. </parent>
  16. <properties>
  17. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  18. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  19. <java.version>1.8</java.version>
  20. </properties>
  21. <dependencies>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-web</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-starter-tomcat</artifactId>
  29. <!--<scope>provided</scope>-->
  30. </dependency>
  31. <dependency>
  32. <groupId>org.springframework.boot</groupId>
  33. <artifactId>spring-boot-starter-test</artifactId>
  34. <scope>test</scope>
  35. </dependency>
  36. <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
  37. <dependency>
  38. <groupId>org.springframework</groupId>
  39. <artifactId>spring-web</artifactId>
  40. </dependency>
  41. <!-- https://mvnrepository.com/artifact/org.eclipse.birt.runtime/org.eclipse.birt.runtime -->
  42. <dependency>
  43. <groupId>org.eclipse.birt.runtime</groupId>
  44. <artifactId>org.eclipse.birt.runtime</artifactId>
  45. <version>4.2.0</version>
  46. </dependency>
  47. <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
  48. <dependency>
  49. <groupId>org.springframework</groupId>
  50. <artifactId>spring-core</artifactId>
  51. </dependency>
  52. <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
  53. <dependency>
  54. <groupId>com.fasterxml.jackson.core</groupId>
  55. <artifactId>jackson-databind</artifactId>
  56. </dependency>
  57. </dependencies>
  58. <build>
  59. <plugins>
  60. <plugin>
  61. <groupId>org.springframework.boot</groupId>
  62. <artifactId>spring-boot-maven-plugin</artifactId>
  63. </plugin>
  64. </plugins>
  65. </build>
展开查看全部
qqrboqgw

qqrboqgw3#

我创建了一个API来使用Sping Boot 的Birt Report。要使用它,只需创建一个新的报告,并通过发送正确的参数来调用它。友情链接:https://github.com/devStraub/BirtAPI
代码:

  1. @RestController
  2. @SpringBootApplication
  3. public class BirtReportApplication {
  4. @Value("${reports.relative.path}")
  5. private String reportsPath;
  6. @Value("${images.relative.path}")
  7. private String imagesPath;
  8. @Value("${temp.files.path}")
  9. private String tempPath;
  10. private ApplicationContext context;
  11. public static void main(String[] args) {
  12. SpringApplication.run(BirtReportApplication.class, args);
  13. }
  14. @SuppressWarnings("unchecked")
  15. @PostMapping("/report")
  16. public ResponseEntity<byte[]> generateReport(@RequestBody String json) {
  17. try {
  18. ObjectMapper objectMapper = new ObjectMapper();
  19. Map<String, String> parametersMap = objectMapper.readValue(json, Map.class);
  20. EngineConfig config = new EngineConfig();
  21. config.getAppContext().put("spring", this.context);
  22. Platform.startup(config);
  23. IReportEngineFactory factory = (IReportEngineFactory) Platform.createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
  24. IReportEngine engine = factory.createReportEngine(config);
  25. IReportRunnable report = engine.openReportDesign(reportsPath + parametersMap.get("report") + ".rptdesign");
  26. IRunAndRenderTask task = engine.createRunAndRenderTask(report);
  27. for (Entry<String, String> parametro : parametersMap.entrySet()) {
  28. task.setParameterValue(parametro.getKey(), parametro.getValue());
  29. }
  30. String reportFileName = tempPath + Calendar.getInstance().getTimeInMillis() + ".pdf";
  31. PDFRenderOption options = new PDFRenderOption();
  32. options.setOutputFormat("pdf");
  33. options.setOutputFileName(reportFileName);
  34. task.setRenderOption(options);
  35. task.run();
  36. task.close();
  37. byte[] relatorioBytes = Files.readAllBytes(Paths.get(reportFileName));
  38. Files.deleteIfExists(Paths.get(reportFileName));
  39. return ResponseEntity.ok()
  40. .header("Content-Type", "application/pdf")
  41. .body(relatorioBytes);
  42. } catch (Exception e) {
  43. e.printStackTrace();
  44. return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
  45. }
  46. }

}
pom.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  5. https://maven.apache.org/xsd/maven-4.0.0.xsd">
  6. <modelVersion>4.0.0</modelVersion>
  7. <parent>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-parent</artifactId>
  10. <version>3.1.0</version>
  11. <relativePath /> <!-- lookup parent from repository -->
  12. </parent>
  13. <groupId>com.birt2</groupId>
  14. <artifactId>birt2</artifactId>
  15. <version>0.0.1-SNAPSHOT</version>
  16. <name>demo</name>
  17. <description>Demo project for Spring Boot</description>
  18. <properties>
  19. <java.version>17</java.version>
  20. </properties>
  21. <dependencies>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-web</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-starter-test</artifactId>
  29. <scope>test</scope>
  30. </dependency>
  31. <dependency>
  32. <groupId>com.innoventsolutions.birt.runtime</groupId>
  33. <artifactId>org.eclipse.birt.runtime_4.8.0-20180626</artifactId>
  34. <version>4.8.0</version>
  35. </dependency>
  36. <dependency>
  37. <groupId>com.fasterxml.jackson.core</groupId>
  38. <artifactId>jackson-databind</artifactId>
  39. <version>2.12.4</version>
  40. </dependency>
  41. <dependency>
  42. <groupId>org.postgresql</groupId>
  43. <artifactId>postgresql</artifactId>
  44. <scope>runtime</scope>
  45. </dependency>
  46. </dependencies>
  47. <build>
  48. <plugins>
  49. <plugin>
  50. <groupId>org.springframework.boot</groupId>
  51. <artifactId>spring-boot-maven-plugin</artifactId>
  52. </plugin>
  53. </plugins>
  54. </build>

您应该打开报表的XML Source并将“version”属性(第2行)修改为“3.2.23”。如果不执行此操作,报告将显示错误。我还在努力解决这个问题如果有人对如何设置报告版本有任何想法,那就太好了。
希望我帮上忙了。

展开查看全部

相关问题