grailsjasper-reportsjasper-plugin

Open report in new tab using jasper plugin in grails


I am using jasper plugin to generate my report. All is fine but except my pdf is opening on the current tab, when I want to open it in a new tab.

I have no idea how to do it. Can anybody please help me on this please?

I am using "i-report" to design my pdf. Here is my code below :

 def index = {
        println(params)
        def testModel = this.getProperties().containsKey('chainModel') ? 
                       chainModel : null

        JasperReportDef report = null
        if (params.containsKey('auto_print_dialog')){

            report = docuJasperService.buildReportDefinition(params,
            request.getLocale(), testModel)
        }
        else{
            report = jasperService.buildReportDefinition(params, 
                     request.getLocale(), testModel)
        }
        generateResponse(report)
    }

    def generateResponse = {reportDef ->
        if (!reportDef.fileFormat.inline && !reportDef.parameters._inline) {
     //response.characterEncoding = "UTF-8"
      //response.setHeader("Content-disposition", 
      //"inline; filename=${params._name}");

          response.setHeader("Content-disposition", "inline; 
          filename="+(reportDef.parameters._name ?: reportDef.name) + 
                   "."+reportDef.fileFormat.extension);
            response.contentType = "application/pdf"
            response.outputStream << reportDef.contentStream.toByteArray()
        } else {

        render(text: reportDef.contentStream, contentType:
               reportDef.fileFormat.mimeTyp,
               encoding: reportDef.parameters.encoding ? 
               reportDef.parameters.encoding : 'UTF-8');

        }
    }

Solution

  • This is going to be handled by the link you use to generate the report. Typically adding a target of _blank will do the trick. For example:

    <g:link controller="myReports" action="whateverReport" target="_blank">Click for your report</g:link>
    

    This will open the link in a new tab.

    If you are using a form adding target to the form will submit the form to a new tab. For example:

    <form name="myForm" action="whateverReport" method="POST" target="_blank">
    

    or

    <g:form name="myForm" controller="myReports" action="whateverReport" target="_blank">
    

    You could even do so with javascript window.open if you needed, but I will leave that example out of this answer as it's well documented elsewhere.