简体   繁体   English

BIRT:如何使用RE API生成和保存PDF格式的报告

[英]BIRT : How to generate and save a report in PDF format using RE API

I am working on a project to generate BIRT reports in PDF format. 我正在研究一个以PDF格式生成BIRT报告的项目。 The application is not meant to be a web application. 该应用程序并不意味着是Web应用程序。 I tried to follow the Report Engine API example on this link http://wiki.eclipse.org/Simple_Execute_(BIRT)_2.1 but I get an error when I run (Run as -> Java Application) the code. 我尝试按照此链接http://wiki.eclipse.org/Simple_Execute_(BIRT)_2.1上的Report Engine API示例进行操作,但是在运行(以-> Java应用程序运行)代码时遇到错误。 My code is as below. 我的代码如下。

My code is as follows: 我的代码如下:

package birt.classicmodels.offices;
import java.util.logging.Level;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.engine.api.EngineConfig;
import org.eclipse.birt.report.engine.api.EngineException;
import org.eclipse.birt.report.engine.api.IReportEngine;
import org.eclipse.birt.report.engine.api.IReportEngineFactory;
import org.eclipse.birt.report.engine.api.IReportRunnable;
import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
import org.eclipse.birt.report.engine.api.PDFRenderOption;

public class ExecuteReport {
    public static void main(String[] args) {
        ExecuteReport er = new ExecuteReport();
        try {
            er.executeReport();
        } catch(Exception ex) {
            System.out.println(ex.toString());
        }
    }

    public void executeReport() throws EngineException {
       IReportEngine engine = null;
       EngineConfig config = null;
       IReportEngineFactory factory = null;
       Object factoryObj = null;
       try {
            config = new EngineConfig();
            config.setBIRTHome("C:\\birt-runtime-4.6.0-20160607\\ReportEngine");
            config.setLogConfig("C:\\temp\\test", Level.FINEST);
            Platform.startup(config);
            factoryObj = Platform.createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
            factory = (IReportEngineFactory) factoryObj;
            engine = factory.createReportEngine(config);      

            // Open the report design
            IReportRunnable design = null;
            String designPath = "C:\\birt-runtime-4.6.0-20160607\\ReportEngine\\samples\\hello_world.rptdesign";
            design = engine.openReportDesign(designPath);
            IRunAndRenderTask task = engine.createRunAndRenderTask(design);      

            PDFRenderOption PDF_OPTIONS = new PDFRenderOption();
            PDF_OPTIONS.setOutputFileName("C:\\temp\\test.pdf");
            PDF_OPTIONS.setOutputFormat("pdf");

            task.setRenderOption(PDF_OPTIONS);
            task.run();
            task.close();
            engine.destroy();
        } catch(final Exception ex) {
            ex.printStackTrace();
        } finally {
            Platform.shutdown();
        }
    }

}

SETUP: 设定:

  1. I installed the BIRT designer using the complete BIRT designer download. 我使用完整的BIRT设计器下载来安装BIRT设计器。
  2. I added all the .jars under the libs folder of the BIRT runtime folder to my build path. 我将BIRT运行时文件夹的libs文件夹下的所有.jars添加到了我的构建路径。
  3. The main method was not part of the example I added it with the intention of getting the report saved to my file system. 主要方法不是该示例的一部分,我添加该示例的目的是将报告保存到我的文件系统中。
  4. The design template I'm referencing is the design example that comes with the BIRT engine. 我引用的设计模板是BIRT引擎随附的设计示例。

ISSUES: 问题:

When I run the code (Run as -> Java Application) I get the following errors: 当我运行代码(运行方式-> Java应用程序)时,出现以下错误:

  1. A pop up dialog with the message: 弹出对话框,显示以下消息:
Java Virtual Machine Launcher
Error: A JNI error has occured, please check your installation and try again
  1. After I click OK on the message dialog, the console is populated with the following error: 在消息对话框上单击“确定”后,控制台将填充以下错误:

     Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/birt/core/framework/PlatformConfig at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Unknown Source) at java.lang.Class.privateGetMethodRecursive(Unknown Source) at java.lang.Class.getMethod0(Unknown Source) at java.lang.Class.getMethod(Unknown Source) at sun.launcher.LauncherHelper.validateMainClass(Unknown Source) at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source) Caused by: java.lang.ClassNotFoundException: org.eclipse.birt.core.framework.PlatformConfig at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) 

I think you are missing following dependency 我认为您缺少依赖关系

<dependency>
    <groupId>org.eclipse.birt.runtime</groupId>
    <artifactId>org.eclipse.birt.runtime</artifactId>
    <version>4.6.0-20160607</version>
</dependency>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM