Birt:如何在独立的 Java 应用程序中从 rtpdesign 文件生成 PDF

问题描述 投票:0回答:2

我从

Eclipse Luna SR2 IDE for Java and Report Developers
设计了非常简单的student.rptdesign文件。它的数据源是
BIRT POJO Data Source
。从那里开始一切正常。 现在我想做的是从 Java 类生成相同的 PDF 文件(没有 BIRT Eclipse)。我搜索了很多文章,但它们大多是关于“JDBC 数据源”的。

任何人都可以指导我该怎么做吗?

java pdf birt
2个回答
3
投票

我想这就是你想要的:

这里是 BIRT 文档中的示例,介绍如何从 Java 主方法调用 ReportEngine,以从现有

.rptdesign
报告生成报告。

里面还有一个

pdf
输出的例子。


0
投票

当您在报告视角中创建报告时,我会在 src 文件夹中创建report.rptdesign,然后从官方网站安装birt,完成后解压并访问插件,将其添加到java应用程序中的类路径中,然后它会工作,你可能会遇到一个错误,说没有找到模块整洁,然后安装tidy.jar。之后享受它,这是一个代码示例,尝试一下。

添加此导入

   import org.eclipse.birt.core.exception.BirtException;
   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.HTMLRenderOption;
   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;
   import java.io.File;

private void printer() throws BirtException {
    Thread printThread = new Thread(() -> {
    IReportEngine engine=null;
    EngineConfig config = null;

    try{
        config = new EngineConfig( );           
        System.out.println("1");
        config.setBIRTHome("C:\\Documents and Settings\\__\\ documents\\Dev\\Programmes\\Runtime BIRT\\4_2_2\\ReportEngine");
        config.setLogConfig(".\\logs", java.util.logging.Level.FINEST);
        System.out.println("2");
        Platform.startup( config );
        System.out.println("3");
        IReportEngineFactory factory = (IReportEngineFactory) Platform
        .createFactoryObject( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );
        engine = factory.createReportEngine( config );      
        System.out.println("4");
        IReportRunnable design = null;
        //Open the report design
        design = engine.openReportDesign("path/to/src/report.rptdesign"); //change it
        IRunAndRenderTask task = engine.createRunAndRenderTask(design);         
        task.setParameterValue("ordParam", (new Integer(10101)));
        task.validateParameters();
        System.out.println("5");
                
        HTMLRenderOption options = new HTMLRenderOption();  
        System.out.println("6");
        options.setOutputFileName("output/report.html");
        options.setOutputFormat("html");
        options.setHtmlRtLFlag(false);
        options.setEmbeddable(false);
        //options.setImageDirectory("C:\\test\\images");

        PDFRenderOption options1 = new PDFRenderOption();
        options1.setOutputFileName("test.pdf");
        options1.setOutputFormat("pdf");
        
        System.out.println("7");

        task.setRenderOption(options1);
        System.out.println("8");
        task.run();
        System.out.println("9");
        task.close();
        System.out.println("10");
        engine.destroy();
        System.out.println("11");
        
        Desktop.getDesktop().open(new File("test.pdf"));
        System.out.println("11");
    }catch( Exception ex){
        ex.printStackTrace();
    }       
    finally
    {
           Platform.shutdown( );
    }
    

    
});

// Start the printing thread
printThread.start();

}

这就是我尝试的方法并且它有效。你可以看到我将函数异步放置,因为它太慢了,所以为了在加载时不使你的应用程序崩溃,我建议这样做,或者如果你愿意的话可以尝试更改 pdf 渲染选项

© www.soinside.com 2019 - 2024. All rights reserved.