Birt将.rptdesign文件报告为Java中的PDF

时间:2018-09-21 10:48:47

标签: birt

我已经从Eclipse设计了.rptdesign文件。它具有XML数据源。从那里开始一切正常。现在,我想从Java应用程序中的.rptdesign生成PDF文件。

有人可以指导我该怎么做吗?

1 个答案:

答案 0 :(得分:1)

BIRT Designer Studio为我们提供了您所正确提及的 .rptdesign 文件。

但是,当我们需要在运行时从java / web应用程序中开发报告时,我们需要:

  1. DEAPI(设计引擎API) :用于在运行时创建设计文件
  2. IReportEngine :用于创建其他格式(     html / pdf。

。)在运行时报告

它是一项总体研究,着重于将BIRT引擎与Java应用程序集成。 请参阅下面的链接/代码以开始学习。

  EngineConfig config = null;

        try{

            config = new EngineConfig( );
            config.setBIRTHome("C:/birt/birt-runtime-2.3RC3/birt-runtime-2_3_0/ReportEngine");
            //config.setLogConfig(null, Level.FINE);
            //config.setResourcePath("C:/work/workspaces/2.3rc3srcexamples/APIs/Reports");
            Platform.startup( config );
            IReportEngineFactory factory = (IReportEngineFactory) Platform
                    .createFactoryObject( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );
            engine = factory.createReportEngine( config );


            IDesignEngineFactory dfactory = (IDesignEngineFactory) Platform
                    .createFactoryObject( IDesignEngineFactory.EXTENSION_DESIGN_ENGINE_FACTORY );
            dengine = dfactory.createDesignEngine( new DesignConfig() );



            IReportRunnable design = null;
            //Open the report design

            SessionHandle session = dengine.newSessionHandle(ULocale.ENGLISH );

         //   design = engine.openReportDesign("C:/Workspace_Reporting/DEAPI/src/deapi/firstTemplate.rpttemplate");
            design = engine.openReportDesign("C:/designTemplate.rpttemplate");

            ReportDesignHandle report = (ReportDesignHandle) design.getDesignHandle( );

            println( " REPORT dataset :"+report.getDataSets());
            // report.saveAs("Template1.rptdesign");
            if(!(params.templateQuery).equals(null))
                changeDataSet(report);
                //ds_interestRateCode

            //Create task to run and render the report,
            IRunAndRenderTask task = engine.createRunAndRenderTask(design);
            task.setParameterValue("templateTitle",params.templateTitle);
            task.validateParameters();

            HTMLRenderOption options = new HTMLRenderOption();
            options.setOutputFileName("C:/Reports/"+params.templateTitle);
            options.setOutputFormat("html");
            options.setImageDirectory("images");

            task.setRenderOption(options);

            task.run();

            task.close();
            session.closeAll(false);

            engine.destroy();
            Platform.shutdown();
            System.out.println("Finished Generation !");
          //  redirect(action: index(),controller: GenericReportGeneratorController)
           renderFile((params.templateTitle))

        }catch( Exception ex){
            ex.printStackTrace();
        }

    }
    def renderFile(def fileName)
    {

        String profileImagePath = "C:/Reports/"
        response.setContentType("APPLICATION/OCTET-STREAM")
        //response.setContentType("text/html;charset=UTF-8")
        response.setHeader("Content-Disposition", "Attachment;Filename="+(fileName))
        def file = new File(profileImagePath+fileName)
        def fileInputStream = new FileInputStream(file)
        def outputStream = response.getOutputStream()
        byte[] buffer = new byte[4096];
        int len;
        while ((len = fileInputStream.read(buffer)) > 0) {
            outputStream.write(buffer, 0, len);
        }
        outputStream.flush()
        outputStream.close()
        fileInputStream.close()

    }

https://wiki.eclipse.org/Java_-_Simple_Design_Engine_API_(BIRT)