Skip to content

Plugin for Grails 3.2+/4+ offering BIRT functionality from legacy Grails 2 BIRT Plugin (http://grails.org/plugin/birt-report)

License

Notifications You must be signed in to change notification settings

paulwellnerbou/grails-birt-plugin

Repository files navigation

grails-birt-plugin

Plugin for Grails 4+ offering BIRT functionality from legacy Grails 2 BIRT Plugin (http://grails.org/plugin/birt-report)


This plugin takes mainly eyck's code from his BIRT Plugin for Grails 2, restructured so that it works with Grails 3 and works with reports from classpath resources, not only from files.

As I am neither an expert for grails nor for BIRT, there may be way better methods to set up this Grails 3 plugin or to initialize the BIRT engine. If so, please contact me, open an issue or create a pull request.


Usage

Setup

  1. Include the dependency in your build.gradle of your Grails 4 project:
repositories {
    maven {
        url  "https://dl.bintray.com/paulwellnerbou/maven" 
    }
}

...

dependencies {
    compile 'org.grails.plugins:birt-report:4.4.0'
}

For Grails 3, use version 4.3.0.4.

  1. Add BirtReportService to your beans in your resources.groovy:
birtReportService(BirtReportService, grailsApplication)
  1. Use BirtReportService in your Controllers/Services, it will be automatically injected:
class MyController {

    BirtReportService birtReportService
    ...
}

Configuration

The location of the reports can be configured in your application.groovy with

birt {
    reportHome = "classpath:Reports"
    useGrailsDatasource = false
    generateAbsoluteBaseURL = false
    baseUrl = ...
    imageUrl = ...
}

This are the default values. Your reports are found in the application's classpath if they are packaged correctly as resources (this is src/main/resources by convention).

I don't really know what the other configuration options are exactly doing, the source code was copied without modifying the logic from Eyck's original sources. Please consider reading the source to find out more. If you found out, please tell me, so I can update the documentation.

If you want to use reports from filesystem, you can use the file: prefix:

birt {
    reportHome = "file:path/to/your/reports"
}

Creating a report

Have a look at the unit test class BirtReportServiceTest, where a sample report is created.

Create a PDF file

def parameters = [:]
def birtReportTmpFile = "anyTmpFileName"
def reportName = "myReport" // .rptdesign is added automatically
def options = birtReportService.getRenderOption(null, 'pdf')
birtReportService.run(reportName, parameters, birtReportTmpFile)
def targetFileName = "/path/to/target/file.pdf"
def result = birtReportService.render(targetFileName, parameters, options)
new File(targetFileName).newOutputStream() << result.toByteArray()

Use it in your servlet context to deliver a PDF as response

def parameters = [:]
def birtReportTmpFile = "anyTmpFileName"
def reportName = "myReport" // .rptdesign is added automatically
def options = birtReportService.getRenderOption(request, 'pdf')
birtReportService.run(reportName, parameters, birtReportTmpFile)
def result = birtReportService.render(targetFileName, parameters, options)
response.setHeader("Content-disposition", "attachment; filename=report.pdf")
response.contentType = 'application/pdf'
response.outputStream << result.toByteArray()

Test, build & install

./gradlew check install

The check task will build and run the tests. The install tasks will package the plugin and publish it to your local Maven repository.

Release

./gradlew release -Prelease.useAutomaticVersion=true -PpublishUser=<user> -PpublishKey=<key>