BIRT-Automated generate pdf report

        建立產生report的engine
        EngineConfig config = new EngineConfig();
        String home = Platform.getInstallLocation().getURL().toString()
                .substring(6);
        log.info("Pdf Report Engine home: " + home);
        config.setEngineHome(home);
        config.setLogConfig(Platform.getInstallLocation().getURL().toString()
                .substring(6)
                + "logs", Level.WARNING);
        pdfReportEngine = new ReportEngine(config);

     
        設定輸出格式
        PDFRenderOption option = new PDFRenderOption();
        option.setOutputFormat("pdf");       
        option.closeOutputStreamOnExit(true);


        String outputName ="test.pdf";
        LOG.info("generate report : "+outputName);
        try {
            OutputStream output = new FileOutputStream(outputName);
            option.setOutputStream(output);
        } catch (FileNotFoundException e) {
            LOG.error("Cannot create report output file "+ e);
            return "";
        }



        BIRT design的檔案
        String designName = "test..rptdesign";
        IReportRunnable runnable = null;
        try {
            runnable = pdfReportEngine.openReportDesign(designName);
        } catch (EngineException e) {
            LOG.error("Design " + designName + " not found! " + e);
            return "";
        }
       
        IRunAndRenderTask task = pdfReportEngine.createRunAndRenderTask(runnable);


  輸入report需要的參數,note:輸入Date type value要將report的parameter type改成date time
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("Ip" , new String("192.168.1.10"));
        parameters.put("Date", new Date("2013/07/29");

        task.setParameterValues(parameters);
        task.setRenderOption(option);
        try {
            task.run();
        } catch (EngineException e) {
            LOG.error(e);
            return "";
        }
        task.close();

參考來源

留言

這個網誌中的熱門文章

Embedded Tomcat

COSCUP2010

SWT SSH terminal