Home » Java » Integrate BIRT in existing webapp

Integrate BIRT in existing webapp

Posted by: admin December 28, 2021 Leave a comment

Questions:

I would like to add the BIRT reporting engine to an existing webapp in Tomcat. I don’t need the BIRT viewer, I really only want to be able to run the reports from a url like http://localhost:8080/birt/output?__report=test.rptdesign&sample=my+parameter and use the different export options pdf, xls, doc, html.

The integration guides I’ve found so far all include the viewer and writing my own servlets to handle different formats.

I was hoping someone knew simply which servlet mappings from the report-engine web.xml file I needed and which jars I would need to include from the lib directory for this barebones BIRT implementation in existing webapp.

Answers:

I was hoping someone knew simply which servlet mappings from the
report-engine web.xml file I needed and which jars I would need to
include from the lib directory for this barebones BIRT implementation
in existing webapp.

I didn’t necessarily want to write my own servlet I just wanted to integrate the existing reporting runtime from its own standalone webapp (here under the “runtime” button) into my existing webapp, so that I don’t have to distribute 2 webapps to support running BIRT reports. Sorry if that wasn’t clearer.

I did work this out though, in the simplest possible fashion, in case anyone has a similar question (using BIRT runtime 3.7.1):

  1. All you need is the following servlet mapping added to your own webapp\web-inf\web.xml file:

    <!-- Engine Servlet -->
    <servlet>
        <servlet-name>EngineServlet</servlet-name>
        <servlet-class>org.eclipse.birt.report.servlet.BirtEngineServlet</servlet-class>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>EngineServlet</servlet-name>
        <url-pattern>/output</url-pattern>
    </servlet-mapping>
    
  2. Include all jars from the web-inf\lib directory of the runtime into your own webapp\web-inf\lib directory.

You can then run .rptdesign files using the output BIRT report url from your own webapp, and specifying whatever format you want, e.g.:

http://localhost:8080/myOwnWebapp/output?__report=test.rptdesign&__format=pdf
http://localhost:8080/myOwnWebapp/output?__report=test.rptdesign&__format=html
http://localhost:8080/myOwnWebapp/output?__report=test.rptdesign&__format=xls
http://localhost:8080/myOwnWebapp/output?__report=test.rptdesign&__format=doc
http://localhost:8080/myOwnWebapp/output?__report=test.rptdesign&__format=ppt

###

As i understand you, you are trying to generate a birt report form a servlet where you have the *.rptdesign in somewhere location.

Good, look at the following code

this.bundle = ResourceBundle.getBundle("com.tts.mersal.resources.MersalResources");
this.config = new EngineConfig();
this.config.setEngineHome(bundle.getString("BIRT_ENGINE_HOME"));
this.config.setLogConfig(bundle.getString("BIRT_LOGGING_FOLDER_PATH"), Level.ALL);
Platform.startup(config);
this.factory = (IReportEngineFactory)Platform.createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
this.engine = factory.createReportEngine( config );
this.engine.changeLogLevel(Level.ALL);
ContentReader contentReader = Repository.getServiceRegistry(FacesContext.getCurrentInstance()).getFileFolderService().getReader(MersalOutboundReportDialogBean.this.dialogReportNode.getNodeRef());
IReportRunnable report = MersalOutboundReportDialogBean.this.getEngine().openReportDesign(contentReader.getContentInputStream());
ReportDesignHandle designHandle = (ReportDesignHandle)report.getDesignHandle();
OdaDataSource source = (OdaDataSource)designHandle.getModule().findDataSource(DATA_SOURCE_NAME);
source.setProperty(source.getPropertyDefn("FILELIST"), buildUrl((String)source.getProperty(designHandle.getModule(), "FILELIST")));
IRunAndRenderTask runAndRenderTask = MersalOutboundReportDialogBean.this.getEngine().createRunAndRenderTask(report);
HTMLRenderOption render = new HTMLRenderOption();
render.setOutputFileName("G:/Render.html");
render.setOutputFormat("html");
runAndRenderTask.setRenderOption(render);
runAndRenderTask.run(); 
runAndRenderTask.close();

As you can see the first thing you must prepare the birt engine and then get an instance of report from type IReportRunnable, so you can after that set the location of the output by using therender option which will be changed based on your request.

You have multiple chocies, HTMLRenderOption, PDFRenderOption and others.

I hope that will serve you.

Thanks.