1

I use jasper report plugin in grails 2.2.4 to generate PDF file. My code for the controller :

import org.codehaus.groovy.grails.plugins.jasper.JasperExportFormat
import org.codehaus.groovy.grails.plugins.jasper.JasperReportDef

class LabController {
    def jasperService
    def directpdf() {

        def reportDef = new JasperReportDef(name:'mbarang.jrxml', fileFormat:JasperExportFormat.PDF_FORMAT)

        response.contentType = 'application/pdf'
        response.outputStream << jasperService.generateReport(reportDef).toByteArray()


        return(false);
    }
}

Those code is working properly in grails 2.2.4. But when I run at grails 2.4.2 I got this error :

Error 500: Internal Server Error
Message
getOutputStream() has already been called for this response

why I got this error?

Alex K
  • 22,315
  • 19
  • 108
  • 236
Julius Prayogo
  • 133
  • 3
  • 12

3 Answers3

0

There are several things you should consider doing to avoid issues with the response/output stream.

First, you should consider flushing the output stream:

response.outputStream.flush()

Second, you should close the output stream:

response.outputStream.close()

Finally, you should be returning null to indicate to Grails you do not want a view rendered.

return null

The above changes will ensure your output stream is handled correctly within your controller method.

Joshua Moore
  • 24,706
  • 6
  • 50
  • 73
  • Thanks for your answer. But why this code can run properly on grails 2.2.4 ? After I debug, the problem seems about the plugins, the `jasperService` cannot call `generateReport` method – Julius Prayogo Jul 17 '14 at 05:45
0

I change the way to send the byteArray I use the render method, so it's become :

render(file: jasperService.generateReport(reportDef).toByteArray(), contentType: 'application/pdf')

But I got another error :

Error 500: Internal Server Error Class java.lang.ClassNotFoundException Message org.apache.commons.collections.ReferenceMap

I think it's about the plugins issue, I think the jasper plugin is incompatible with grails 2.4.2. I decided to use the jasper library directly. Copy the required .jar to lib/ folder. I download this .jar :

commons-beanutils-1.9.2.jar commons-collections-3.2.jar commons-digester-2.1.jar commons-logging-1.2.jar itext-2.1.7.jar jasperreports-5.6.0.jar

Then change my controller become, something like this :

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRPdfExporter;

class LaporanController {
    def printbarang() {
        try {
            String reportName = "c:/xampp/halo"
            // compiles jrxml
            JasperCompileManager.compileReportToFile(reportName + ".jrxml");
            // fills compiled report with parameters and a connection
            // JasperPrint print = JasperFillManager.fillReport(reportName + ".jasper", parameters, connection);
            JasperPrint print = JasperFillManager.fillReport(reportName + ".jasper", null);
            ByteArrayOutputStream  pdfStream = new ByteArrayOutputStream();
            // exports report to pdf
            JRExporter exporter = new JRPdfExporter();
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
            // exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, new FileOutputStream(reportName + ".pdf")); // your output goes here
            exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, pdfStream); // your output goes here
            exporter.exportReport();
        } catch (Exception e) {
            render('something when wrong')
            throw new RuntimeException("It's not possible to generate the pdf report.", e); 
        } finally {
            render(file: pdfStream.toByteArray(), contentType: 'application/pdf')
        }
    }
}
Julius Prayogo
  • 133
  • 3
  • 12
  • I had a similar problem when I went from grails 2.3.11 to 2.4.3. I tried the render statement and got an error saying that org.apache.commons.beanutils.MethodUtils was not defined. So, I added a dependency on commons-beanutils (commons-beanutils:commons-beanutils:1.9.2) and it worked. It I already had commons-collections dependency, which is probably what you needed to resolve the error you got when you used the render statement. – Nathan Ward Oct 22 '14 at 12:09
0

This is fixed in 1.11.0-SNAPSHOT:

https://jira.grails.org/browse/GPJASPER-73

Ingo Kegel
  • 46,523
  • 10
  • 71
  • 102