How to Use BIRT Report with Spring Boot Application

Hello friends! Today, I am going to share with you how we can use BIRT Report and integrate it with our Spring Boot application. We will keep things simple and easy to understand. So, let’s get started.

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.

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:
Code Copy Example

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:
Code Copy Example

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 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

Code Copy Example

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

Code Copy Example

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://localhost:8080/generateReport?reportName=your_report_file.rptdesign. Your BIRT report will be generated and displayed as a PDF.

Conclusion

That’s it! You have successfully integrated BIRT Report with your Spring Boot application. I hope this guide was easy to follow. If you have any questions related to BIRT Report or run into any issues, feel free to ask in the comments below.