从Netbeans应用程序生成BIRT PDF

时间:2012-02-15 18:16:43

标签: java pdf-generation birt

我使用Eclipse的Eclipse BIRT插件创建了一个报告,并希望从控制台应用程序创建一个PDF(我使用Netbeans来创建它)

我查看了文档但我无法真正得到任何工作。如何调用BIRT从.rptdesign生成PDF?

1 个答案:

答案 0 :(得分:2)

我从eclipse网站获得了大部分代码,但我现在似乎无法找到它。我在下面注释了我的实现特定代码,但其余代码应该可用/可编辑。所有导入都来自正常的birt运行时。下面的printReport方法会从报告网址生成PDF:

import java.io.ByteArrayOutputStream;
import java.util.Collection;
import java.util.Iterator;
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.IGetParameterDefinitionTask;
import org.eclipse.birt.report.engine.api.IParameterDefnBase;
import org.eclipse.birt.report.engine.api.IParameterGroupDefn;
import org.eclipse.birt.report.engine.api.IParameterSelectionChoice;
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.IScalarParameterDefn;
import org.eclipse.birt.report.engine.api.PDFRenderOption;

public class BirtEngine {

    IReportEngine engine = null;
    EngineConfig config = null;

    public BirtEngine()
    {
        try {
            config = new EngineConfig( );
            config.setBIRTHome("C:\\birtruntime\\ReportEngine");
            Platform.startup( config );
            IReportEngineFactory factory = (IReportEngineFactory) Platform.createFactoryObject( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );
            engine = factory.createReportEngine( config );
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public void openReport(String report)
    {
        try {
      IReportRunnable design = null;
            design = engine.openReportDesign(report);
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }

    public void renderReport(String report)
    {
        try {
      IReportRunnable design = null;
            design = engine.openReportDesign(report);
            IRunAndRenderTask task = engine.createRunAndRenderTask(design);
            HTMLRenderOption options = new HTMLRenderOption();
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            options.setOutputStream(bos);
            options.setOutputFormat("html");
            options.setEmbeddable(true);
            task.setRenderOption(options);
            task.run();
            task.close();
            //TreeBirtFrameView.jEditorPane1.setContentType("text/html");
            //TreeBirtFrameView.jEditorPane1.setText(bos.toString());
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }

    public void printReport(String report)
    {
        try {
      IReportRunnable design = null;
            design = engine.openReportDesign(report);
            IRunAndRenderTask task = engine.createRunAndRenderTask(design);
            PDFRenderOption options = new PDFRenderOption();
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            options.setOutputStream(bos);
            options.setOutputFormat("pdf");
            task.setRenderOption(options);
            task.run();
            task.close();
      //Runtime.getRuntime().exec("\\\\myServer\\pgms$\\Adobe\\Reader 9.0\\Reader\\acrord32.exe report.pdf");
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }

    public void stopPlatform()
    {
        engine.destroy();
        Platform.shutdown();
    }
}