grailsjasper-reportsjasper-plugin

Grails JasperService: how do I pass parameters to a report?


I have a simple Jasper report and a Grails app that uses jasper plugin to generate a report. The report has a parameter with a default value. Say, its name is user_id When I print the report, the default value is used and the result is ok.

Now I wish to be able to pass my own parameter value to the report. Here is the code I use to get my report:

import org.codehaus.groovy.grails.plugins.jasper.JasperExportFormat
import org.codehaus.groovy.grails.plugins.jasper.JasperReportDef
...
{   
    // def user_id = 1
    def reportDef = new JasperReportDef(name:'iResume.jasper', fileFormat:JasperExportFormat.DOCX_FORMAT)
    def file = jasperService.generateReport(reportDef).toByteArray()
}

How do I pass a param, for example user_id into the report?


Solution

  • Finally managed to do it with this code:

        params.put("_file", "iResume.jasper")
        params.put("_format", "DOCX")
        params.put("user_id", 1)
    
        println params
    
        def reportDef = jasperService.buildReportDefinition(params, request.getLocale(), [])
        def file = jasperService.generateReport(reportDef).toByteArray()
    

    I took this from the Jasper taglib and the controller code. This is kind of Voodoo programming cause I don't know what the last param [] is for, but it works. Will be grateful if anyone can clarify what's going on here.