Free 30-minute AI & automation audit with a senior engineer. You keep the notes. Book yours

Backend Development

BIRT Reports with Spring Boot: Integration Guide

Learn how to integrate BIRT reports into your Spring Boot application. A guide to setting up, configuring, and utilizing BIRT for robust reporting solutions

BIRT Reports with Spring Boot: Integration Guide

How to Use BIRT Report with Spring Boot Application

This guide shows how to use BIRT Report with a Spring Boot application. We will keep things simple and easy to understand.

What is BIRT Report?
BIRT stands for Business Intelligence and Reporting Tools. BIRT Report is open source tool to create report and provides business intelligence capabilities. You can create complex and professional reports using BIRT. It is widely used in Java applications.

Setting Up BIRT in Your Spring Boot Project
First, we need to add BIRT dependencies to our Spring Boot project. We will use Maven for dependency management.

Step 1: Add Dependencies
Add following dependencies in your pom.xml.

<dependencies>
    <!-- Spring Boot dependencies -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>com.innoventsolutions.birt.runtime</groupId>
        <artifactId>org.eclipse.birt.runtime_4.8.0-20180626</artifactId>
        <version>4.8.0</version>
    </dependency>
</dependencies>

Step 2: Create a BIRT Report Design
Next, we need to create a BIRT report design. You can use the BIRT Report Designer tool for this. Download and install the BIRT Report Designer from the official BIRT website. Create a new report and design it according to your requirements. Save the report file with a .rptdesign extension.

Step 3: Configure BIRT Engine
We need to configure the BIRT Engine in our Spring Boot application. Create a new configuration class:

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.IReportEngine;
import org.eclipse.birt.report.engine.api.IReportEngineFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class BirtConfig {
    @Bean
    public IReportEngine reportEngine() {
        EngineConfig config = new EngineConfig();
        config.setEngineHome("");
        try {
            Platform.startup(config);
        } catch (BirtException e) {
            throw new IllegalStateException("Failed to start BIRT platform", e);
        }
        IReportEngineFactory factory =
            (IReportEngineFactory) Platform.createFactoryObject
        (IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
        IReportEngine engine = factory.createReportEngine(config);
        engine.changeLogLevel(Level.ALL);
        return engine;
    }
}

Step 4: Create a Controller to Generate Reports
Create a new controller class to handle report generation:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

@RestController
public class ReportController {

    private final ReportService reportService;

    @Autowired
    public ReportController(ReportService reportService) {
        this.reportService = reportService;
    }

    @GetMapping("/generateReport")
    public ResponseEntity<InputStreamResource> generateReport(@RequestParam String reportName) {
        try {
            ByteArrayOutputStream outputStream = reportService.createBirtPdf(reportName);

            ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());

            HttpHeaders headers = new HttpHeaders();
            headers.add(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=" + reportName + ".pdf");

            return ResponseEntity.ok()
                    .headers(headers)
                    .contentType(MediaType.APPLICATION_PDF)
                    .body(new InputStreamResource(inputStream));
        } catch (Exception e) {
            // Log the exception and return an appropriate response
            return ResponseEntity.status(500).build();
        }
    }
}

Step 5: Create a Service to Generate Reports

java

import org.eclipse.birt.report.engine.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletContext;
import java.io.ByteArrayOutputStream;
import java.io.File;

@Service
public class ReportService {

    private static final String SRC_DIR = "/report";
    public static final String EXT_PDF = ".pdf";
    public static final String EXT_RPT_DESIGN = ".rptdesign";
    public static final String UNDERSCORE = "_";

    private final ServletContext servletContext;
    private final IReportEngine reportEngine;
    private String path;

    @Autowired
    public ReportService(ServletContext servletContext, IReportEngine reportEngine) {
        this.servletContext = servletContext;
        this.reportEngine = reportEngine;
        this.path = servletContext.getRealPath(SRC_DIR);
    }

    public ByteArrayOutputStream createBirtPdf(String reportName) throws Exception {
        RenderOption renderOption = new PDFRenderOption();
        renderOption.setOutputFormat(RenderOption.OUTPUT_FORMAT_PDF);

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        renderOption.setOutputStream(outputStream);

        IReportRunnable runnableTask =
                reportEngine.openReportDesign(path + File.separator + reportName + EXT_RPT_DESIGN);
        IRunAndRenderTask task = reportEngine.createRunAndRenderTask(runnableTask);

        task.setRenderOption(renderOption);
        task.run();
        task.close();

        return outputStream;
    }
}

Step 6: Create a Custom Exception for Report Generation

public class ReportGenerationException extends RuntimeException {
    public ReportGenerationException(String message, Throwable cause) {
        super(message, cause);
    }
}

Step 7: Run the Application
Now, run your Spring Boot application. Open your browser and go to http://multigenesys.com/generateReport?reportName=your_report_file.rptdesign. Your BIRT report will be generated and displayed as a PDF.

Conclusion

BIRT Report is now integrated with your Spring Boot application. If you have questions about BIRT Report or run into issues, contact us.

Newsletter

Get the MultiGenesys newsletter

Practical notes on AI, software engineering and automation from our engineers. One email a month, no spam, unsubscribe anytime.

By subscribing you agree to receive our newsletter. Unsubscribe anytime. See our privacy policy.

Free AI audit Book a call