Sunday, July 20, 2014

Grails using jasper report without plugin

This snippet is used to generate .pdf file and the send the byte array to the browser, so the user can print the .pdf directly to the printer. I use jasper report to generate the .pdf file. My grails version is 2.4.2. First, download the necessary .jar file, I use this version of 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

You can download from this link : https://drive.google.com/file/d/0B3BD2OiyeQ3UQWN2Nk82R1dhYmM/edit?usp=sharing

After download the .jar files, put those .jar file to the /lib folder. Then I use this code in my controller :

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

    def dataSource
    
    def print() {
     try {
      String reportName, namaFile, dotJasper
      namaFile = "barang"
      reportName = grailsApplication.mainContext.getResource('reports/' + namaFile + '.jrxml').file.getAbsoluteFile()
      dotJasper = grailsApplication.mainContext.getResource('reports/' + namaFile + '.jasper').file.getAbsoluteFile()

      // Report parameter
      Map reportParam = new HashMap()

      // compiles jrxml
      JasperCompileManager.compileReportToFile(reportName);
      // fills compiled report with parameters and a connection
      JasperPrint print = JasperFillManager.fillReport(dotJasper, reportParam, dataSource.getConnection());

      ByteArrayOutputStream  pdfStream = new ByteArrayOutputStream();

      // exports report to pdf
      JRExporter exporter = new JRPdfExporter();
      exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
      exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, pdfStream); // your output goes here
   
      exporter.exportReport();

     } catch (Exception e) {
   
      throw new RuntimeException("It's not possible to generate the pdf report.", e); 
     } finally {
      render(file: pdfStream.toByteArray(), contentType: 'application/pdf')
     }
    }
}

14 comments:

  1. Hi,
    I am using grails 3.0.5 and include the jasper jar manually

    compile('com.lowagie:itext:2.1.7')
    compile ('net.sf.jasperreports:jasperreports:6.2.1')

    I followed your sample code and run without error, but the only proboem is the gsp page shows blank pdf.

    can i have a sample of the gsp code? thanks.

    ReplyDelete
    Replies
    1. Blank pdf is good. Make sure the SQL query return some data with the given parameter.

      Delete
    2. Hi , I am getting the following error. Please help. Thanks
      URI
      /jasReport/index
      Class
      groovy.lang.MissingPropertyException
      Message
      No such property: pdfStream for class: jasper2.JasReportController

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. in grails 3.1.7 i can not integrate it ... is there any suggestion

    ReplyDelete
    Replies
    1. Sorry, I haven't tried grails 3.x yet

      Delete
    2. maybe you can try this https://javagrails.wordpress.com/2016/06/03/grails-3-jasper-integration/

      Step 1 is a bit incorrect for my Grails 3.0.8. But the rest of the steps are all ok.

      Delete
    3. Hi, I am getting the following error. Please hep. Thanks.
      URI
      /jasReport/index
      Class
      groovy.lang.MissingPropertyException
      Message
      No such property: pdfStream for class: jasper2.JasReportController

      Delete
  4. thanks,
    for grails 3.1.x
    update build.gradle
    repositories {
    mavenCentral()
    mavenLocal()
    maven { url "https://repo.grails.org/grails/core" }
    mavenCentral artifactUrls: [
    'http://jasperreports.sourceforge.net/maven2',
    'http://jaspersoft.artifactoryonline.com/jaspersoft/third-party-ce-artifacts/'
    ]
    }
    dependencies{
    ...
    compile 'com.lowagie:itext:2.1.7'
    compile 'net.sf.jasperreports:jasperreports:6.2.2'
    }

    ReplyDelete
    Replies
    1. Thanks... will be useful when upgrading my Grails

      Delete
  5. Hi am new in grails. what will be gsp code for it? could you please

    ReplyDelete
  6. Hi am new in grails. what will be gsp code for it? could you please

    Actually my problem is :
    http://stackoverflow.com/questions/38493438/how-can-i-generate-jasper-report-in-grails-with-print-option

    ReplyDelete
  7. In grails 3.1.8 i can not integrate it. Is there any help or idea. Please help me.

    ReplyDelete
    Replies
    1. Hi, I am getting the following error. Please help. Thanks.
      URI
      /jasReport/index
      Class
      groovy.lang.MissingPropertyException
      Message
      No such property: pdfStream for class: jasper2.JasReportController

      Delete