Hot questions for Using JasperReports in spring

Question:

I am working on the Spring MongoDB Jasper integration example. I've had the Spring Mysql Jasper example which is working fine. The same program I am looking to convert for the mongodb.

Source code at : https://github.com/test512/spring-mvc-mongo-jasper.git

@Note: I followed http://jasperreports.sourceforge.net/api/index.html, but I dont see jar file to download. Its not present in maven repo?

LoadJasperReport.java

@Controller
public class LoadJasperReport {
    private static final Logger LOGGER = LoggerFactory.getLogger(LoadJasperReport.class);

    @ModelAttribute("jasperRptFormats")
    public ArrayList<String> getJasperRptFormats() {
        ArrayList<String> jasperRptFormats = new ArrayList<String>();
        jasperRptFormats.add("Html");
        jasperRptFormats.add("PDF");

        return jasperRptFormats;
    }

    @RequestMapping(value = "/loadJasper", method = RequestMethod.GET)
    public String loadSurveyPg(@ModelAttribute("jasperInputForm") JasperInputForm jasperInputForm, Model model) {
        model.addAttribute("JasperInputForm", jasperInputForm);
        return "loadJasper";
    }

    @RequestMapping(value = "/generateReport", method = RequestMethod.POST)
    public String generateReport(@Valid @ModelAttribute("jasperInputForm") JasperInputForm jasperInputForm,
            BindingResult result, Model model, HttpServletRequest request, HttpServletResponse response)
            throws ParseException {
        LOGGER.debug("~~~ Generate Report ~~~");

        if (result.hasErrors()) {
            LOGGER.error("validation error occured in jasper input form");
            return "loadJasper";

        }

        String reportFileName = "JREmp1";

        Connection conn = null;
        try {
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                LOGGER.error("Please include Classpath Where your MySQL Driver is located");
                e.printStackTrace();
            }

            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
            if (conn != null)
                LOGGER.debug("Database Connected");
            else
                LOGGER.debug(" connection Failed ");

            String rptFormat = jasperInputForm.getRptFmt();
            String noy = jasperInputForm.getNoofYears();

            LOGGER.debug("rpt format " + rptFormat + ", no of years " + noy);

            HashMap<String, Object> hmParams = new HashMap<String, Object>();
            hmParams.put("noy", new Integer(noy));
            hmParams.put("Title", "Employees working more than " + noy + " Years");

            JasperReport jasperReport = getCompiledFile(reportFileName, request);

            if (rptFormat.equalsIgnoreCase("html")) {
                JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, hmParams, conn);
                // For HTML report
                generateReportHtml(jasperPrint, request, response);
            } else if (rptFormat.equalsIgnoreCase("pdf")) {
                // For PDF report
                generateReportPDF(response, hmParams, jasperReport, conn);
            }
        } catch (Exception sqlExp) {
            LOGGER.error("Exception::" + sqlExp.toString());
        } finally {
            try {
                if (conn != null) {
                    conn.close();
                    conn = null;
                }
            } catch (SQLException expSQL) {
                LOGGER.error("SQLExp::CLOSING::" + expSQL.toString());
            }
        }
        return null;
    }

    private JasperReport getCompiledFile(String fileName, HttpServletRequest request) throws JRException {
        LOGGER.debug("path " + request.getSession().getServletContext().getRealPath("/jasper/" + fileName + ".jasper"));

        File reportFile = new File(
                request.getSession().getServletContext().getRealPath("/jasper/" + fileName + ".jasper"));

        // If compiled file is not found, then compile XML template
        if (!reportFile.exists()) {
            JasperCompileManager.compileReportToFile(
                    request.getSession().getServletContext().getRealPath("/jasper/" + fileName + ".jrxml"),
                    request.getSession().getServletContext().getRealPath("/jasper/" + fileName + ".jasper"));
        }

        JasperReport jasperReport = (JasperReport) JRLoader.loadObjectFromFile(reportFile.getPath());
        return jasperReport;
    }

    private void generateReportHtml(JasperPrint jasperPrint, HttpServletRequest req, HttpServletResponse resp)
            throws IOException, JRException {
        LOGGER.debug("~~~ Generate HTML Report ~~~");

        HtmlExporter exporter = new HtmlExporter();

        List<JasperPrint> jasperPrintList = new ArrayList<JasperPrint>();
        jasperPrintList.add(jasperPrint);

        exporter.setExporterInput(SimpleExporterInput.getInstance(jasperPrintList));
        exporter.setExporterOutput(new SimpleHtmlExporterOutput(resp.getWriter()));

        SimpleHtmlReportConfiguration configuration = new SimpleHtmlReportConfiguration();
        exporter.setConfiguration(configuration);
        exporter.exportReport();
    }

    private void generateReportPDF(HttpServletResponse resp, Map<String, Object> parameters, JasperReport jasperReport,
            Connection conn) throws JRException, NamingException, SQLException, IOException {
        LOGGER.debug("~~~ Generate PDF Report ~~~");

        byte[] bytes = null;
        bytes = JasperRunManager.runReportToPdf(jasperReport, parameters, conn);

        resp.reset();
        resp.resetBuffer();
        resp.setContentType("application/pdf");
        resp.setContentLength(bytes.length);
        ServletOutputStream ouputStream = resp.getOutputStream();
        ouputStream.write(bytes, 0, bytes.length);
        ouputStream.flush();
        ouputStream.close();
    }
}

This program I am trying to convert for the MongoDB like below, but I am not sure how can I get the Connection object? Is there any way to get the connection object from the mongodb or any other way to write code for the mongodb jasper?

Now I see the issue at line :

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, hmParams, conn);

I changed code like below but its not working. Please guide.

package net.javaonline.spring.jasper.controller;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.naming.NamingException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.jaspersoft.mongodb.MongoDbDataSource;
import com.jaspersoft.mongodb.connection.MongoDbConnection;

import net.javaonline.spring.jasper.form.JasperInputForm;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.engine.export.HtmlExporter;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleHtmlExporterOutput;
import net.sf.jasperreports.export.SimpleHtmlReportConfiguration;

@Controller
public class LoadJasperReport {
    private static final Logger LOGGER = LoggerFactory.getLogger(LoadJasperReport.class); 

    @ModelAttribute("jasperRptFormats")
    public ArrayList<String> getJasperRptFormats(){
        ArrayList<String> jasperRptFormats = new ArrayList<String>();
        jasperRptFormats.add("Html");
        jasperRptFormats.add("PDF");

        return jasperRptFormats;
    }   


    @RequestMapping(value = "/loadJasper", method = RequestMethod.GET)
    public String loadSurveyPg(
            @ModelAttribute("jasperInputForm") JasperInputForm jasperInputForm,
            Model model) {
        model.addAttribute("JasperInputForm", jasperInputForm);
        return "loadJasper";
    }


    @RequestMapping(value = "/generateReport", method = RequestMethod.POST)
    public String generateReport(
            @Valid @ModelAttribute("jasperInputForm") JasperInputForm jasperInputForm,
            BindingResult result,Model model, 
            HttpServletRequest request, HttpServletResponse response) throws ParseException {
        LOGGER.debug("~~~ Generate Report ~~~");

        if (result.hasErrors()) {
            LOGGER.error("validation error occured in jasper input form");
            return "loadJasper";

        }

        String reportFileName = "JREmp1";
        MongoDbConnection conn = null;

        try {
            try {
                conn = new MongoDbConnection("mongodb://localhost:27017/mydb", null, null);
            } catch (JRException e) {
                System.out.println("JREException : "+e.getMessage());
            }

            Map<String, Object> parameters = new HashMap<String, Object>();
            parameters.put(MongoDbDataSource.QUERY_LANGUAGE, conn);

            String rptFormat = jasperInputForm.getRptFmt();
            String noy = jasperInputForm.getNoofYears();

            LOGGER.debug("rpt format " + rptFormat+", no of years " + noy);


            HashMap<String,Object> hmParams=new HashMap<String,Object>();
            hmParams.put("noy", new Integer(noy));
            hmParams.put("Title", "Employees working more than "+ noy + " Years");


            JasperReport jasperReport = getCompiledFile(reportFileName, request);

            if (rptFormat.equalsIgnoreCase("html") ) {
                JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, hmParams, conn);
                // For HTML report
                generateReportHtml(jasperPrint, request, response); 
            }
            else if(rptFormat.equalsIgnoreCase("pdf")){
                // For PDF report
                generateReportPDF(response, hmParams, jasperReport, conn); 
            }
        } catch (JRException | IOException | NamingException | SQLException e) {
            System.out.println(e.getMessage());
        } finally {
            if (conn != null) {
                conn.close();
                conn = null;
            }
        }
        return null;
    }

    private JasperReport getCompiledFile(String fileName, HttpServletRequest request) throws JRException {
        LOGGER.debug("path " + request.getSession().getServletContext().getRealPath("/jasper/" + fileName + ".jasper"));

        File reportFile = new File( request.getSession().getServletContext().getRealPath("/jasper/" + fileName + ".jasper"));

        // If compiled file is not found, then compile XML template
        if (!reportFile.exists()) {
            JasperCompileManager.compileReportToFile(request.getSession().getServletContext().getRealPath("/jasper/" + fileName + ".jrxml"),request.getSession().getServletContext().getRealPath("/jasper/" + fileName + ".jasper"));
        }

        JasperReport jasperReport = (JasperReport) JRLoader.loadObjectFromFile(reportFile.getPath());
        return jasperReport;
    } 


    private void generateReportHtml( JasperPrint jasperPrint, HttpServletRequest req, HttpServletResponse resp) 
            throws IOException, JRException {
        LOGGER.debug("~~~ Generate HTML Report ~~~");

        HtmlExporter exporter=new HtmlExporter();

        List<JasperPrint> jasperPrintList = new ArrayList<JasperPrint>();
        jasperPrintList.add(jasperPrint);

        exporter.setExporterInput(SimpleExporterInput.getInstance(jasperPrintList));
        exporter.setExporterOutput( new SimpleHtmlExporterOutput(resp.getWriter()));

        SimpleHtmlReportConfiguration configuration =new SimpleHtmlReportConfiguration();
        exporter.setConfiguration(configuration);
        exporter.exportReport();
    }

    private void generateReportPDF (HttpServletResponse resp, Map<String, Object> parameters, 
            JasperReport jasperReport, Connection conn)throws JRException, NamingException, SQLException, IOException {
        LOGGER.debug("~~~ Generate PDF Report ~~~");

        byte[] bytes = null;
        bytes = JasperRunManager.runReportToPdf(jasperReport, parameters, conn);

        resp.reset();
        resp.resetBuffer();
        resp.setContentType("application/pdf");
        resp.setContentLength(bytes.length);
        ServletOutputStream ouputStream = resp.getOutputStream();
        ouputStream.write(bytes, 0, bytes.length);
        ouputStream.flush();
        ouputStream.close();
    } 
}

JREmp1.jrxml

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="FirstReport" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="7ae7f135-7061-4947-be6e-66935e65141a">
    <property name="ireport.zoom" value="1.2100000000000006"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <queryString language="MongoDbQuery">
        <![CDATA[{ collectionName : 'oms_order' }]]>
    </queryString>
    <field name="Emp_code" class="java.lang.String"/>
    <field name="EmpName" class="java.lang.String"/>
    <field name="Salary" class="java.lang.Integer"/>
    <field name="Doj" class="java.lang.String"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="31" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="0" width="555" height="27" uuid="52aef86d-1b56-49fa-bb20-e4b0ad93e554"/>
                <textElement textAlignment="Center">
                    <font size="15"/>
                </textElement>
                <text><![CDATA[OMS Order Report]]></text>
            </staticText>
        </band>
    </title>
    <pageHeader>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement x="435" y="0" width="80" height="20" uuid="1130b204-cf50-4ca0-a8ad-90377eb3c0f4"/>
                <textElement textAlignment="Right"/>
                <textFieldExpression><![CDATA["Page "+$V{PAGE_NUMBER}+" of"]]></textFieldExpression>
            </textField>
            <textField evaluationTime="Report">
                <reportElement x="515" y="0" width="40" height="20" uuid="9a913f85-d984-4d46-bc15-6223ad0746c3"/>
                <textFieldExpression><![CDATA[" " + $V{PAGE_NUMBER}]]></textFieldExpression>
            </textField>
        </band>
    </pageHeader>
    <columnHeader>
        <band height="23" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="0" width="100" height="20" uuid="14c1104f-267c-4a8e-90f9-4290ef574993"/>
                <text><![CDATA[buySideClientCode]]></text>
            </staticText>
            <staticText>
                <reportElement x="468" y="0" width="87" height="20" uuid="f4886a82-d127-4bd8-8561-8f4571763f98"/>
                <text><![CDATA[destId]]></text>
            </staticText>
            <staticText>
                <reportElement x="268" y="0" width="100" height="20" uuid="35a5e86f-5d4c-4be2-be09-e2629a2a089b"/>
                <text><![CDATA[orderAction]]></text>
            </staticText>
            <staticText>
                <reportElement x="100" y="0" width="100" height="20" uuid="322fe843-6a8f-4e08-b4bd-15ebf89c4715"/>
                <text><![CDATA[clOrdID]]></text>
            </staticText>
            <staticText>
                <reportElement x="368" y="0" width="100" height="20" uuid="c1f05f4c-5f81-43c3-9861-f33d11692874"/>
                <text><![CDATA[orderID]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="23" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="100" height="20" uuid="bce7874d-e031-4c70-86ed-22f6c0033561"/>
                <textFieldExpression><![CDATA[$F{buySideClientCode}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="468" y="0" width="87" height="20" uuid="87a25154-2e8c-4bb6-9ef3-a188577b04f6"/>
                <textFieldExpression><![CDATA[$F{destId}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="368" y="2" width="100" height="20" uuid="f65ef2db-2117-4bb1-92a6-ac1f0b2f18ef"/>
                <textFieldExpression><![CDATA[$F{orderID}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="100" y="2" width="100" height="20" uuid="88f63e67-0dd6-4e76-a428-c610b3d0ede0"/>
                <textFieldExpression><![CDATA[$F{clOrdID}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="268" y="3" width="100" height="20" uuid="52daf5f7-09e8-492d-bc46-2d5ebfd13838"/>
                <textFieldExpression><![CDATA[$F{orderAction}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <columnFooter>
        <band height="30" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="19" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="22" splitType="Stretch"/>
    </summary>
</jasperReport>


Answer:

Anyway I was able to solve this issue now. You need below dependency:

<!-- but you need one more (secret) from mongoDB site -->
        <dependency>
            <groupId>com.jaspersoft.connectors.mongodb</groupId>
            <artifactId>js-mongodb-datasource</artifactId>
            <version>0.9.3</version>
        </dependency>

And

<!-- This is a secret repository for js-mongodb-datasource jar -->
    <repositories>
        <repository>
            <id>MongoJasperRepo</id>
            <url>http://jaspersoft.artifactoryonline.com/jaspersoft/jaspersoft-repo</url>
            <releases>
                <enabled>true</enabled>
            </releases>
        </repository>
    </repositories>

and just used the below code. Done !!

MongoDbConnection conn = null;

        try {
            try {
                conn = new MongoDbConnection("mongodb://" + mongoHost + ":" + mongoPort + "/" + mongodb, null, null);
            } catch (JRException e) {
                System.out.println("JREException : " + e.getMessage());
            }

Question:

I'm developing web-application project which is using Apache Tomcat, Spring and Hibernate. It also uses JasperReports v.6.4.0 to generate pdf-reports. Recently, the versions of Spring and Hibernate were updated from 3.1.2 to 4.3.6 for Spring and from 3.3.1 to 5.2.8 for Hibernate.

Every problem that came after migration was solved except one. When reports with hql-queries are generated the following exception is thrown:

java.lang.NoSuchMethodError: org.hibernate.Session.createQuery(Ljava/lang/String;)Lorg/hibernate/Query;
at net.sf.jasperreports.engine.query.JRHibernateQueryExecuter.createQuery(JRHibernateQueryExecuter.java:279)
at net.sf.jasperreports.engine.query.JRHibernateQueryExecuter.createDatasource(JRHibernateQueryExecuter.java:195)
at net.sf.jasperreports.engine.fill.JRFillDataset.createQueryDatasource(JRFillDataset.java:1245)
at net.sf.jasperreports.engine.fill.JRFillDataset.initDatasource(JRFillDataset.java:723)
at net.sf.jasperreports.engine.fill.BaseReportFiller.setParameters(BaseReportFiller.java:438)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:550)
at net.sf.jasperreports.engine.fill.BaseReportFiller.fill(BaseReportFiller.java:396)
at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:90)
at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:456)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:863)

Before upgrading, JasperReports v.6.4.0 was working fine with Hibernate v.3.3.1. Can anybody help me to avoid this problem?


Answer:

The JRHibernateQueryExecuter in jasper-reports v.6.4.0 is depending on hibernate v. 3.3.2.GA.

The difference is that in Hibernate 5.2, the Query class moved from org.hibernate to org.hibernate.query

So what is the solution?

To use JRHibernateQueryExecuter you need hibernate v. 3.3.2 in classpath, so either you revert back or you develop your own JRHibernateQueryExecuter and JRHibernateQueryExecuterFactory that depends on Hibernate 5.2.8.

Developing your own JRHibernateQueryExecuter is probably not as difficult as it may sound since the source code is available, you would probably just need to tweak the imports.

To register your "new" QueryFactory see: How I can associate a query language with my executer in Jaspersoft studio?

Question:

My stack

Spring boot 1.5.6

JasperReport

Ajax JQuery 3.1.1

My goal

I m trying to print for first time a report using jasper report, I have a printing service where I have my reports stored, my idea was to send http request via ajax containing datas and get a pdf report

What I tried

I have a rest controller in my spring-boot backend wich is implmented like this

@RestController
@RequestMapping(PrintController.API)
public class PrintController {
    public static final String API="print";

    @PostMapping("client")
    public void export(@RequestBody List<ClientJsonDto> datas,HttpServletResponse response){

        System.out.println(datas);
         JRBeanCollectionDataSource itemsJRBean = new JRBeanCollectionDataSource(datas);

    /* Map to hold Jasper report Parameters */
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("ItemDataSource", itemsJRBean);

    byte[] bytes = generatePDFReport("refclient", parameters);
    return ResponseEntity
              .ok()
              // Specify content type as PDF
              .header("Content-Type", "application/pdf; charset=UTF-8")
              // Tell browser to display PDF if it can
              .header("Content-Disposition", "attachment;inline; filename=\"client.pdf\"")
              .body(bytes);

    }


public byte[] generatePDFReport(String inputFileName, Map<String, Object> params) {
    return generatePDFReport(inputFileName, params, new JREmptyDataSource());
}


public File loadJasperFile(String file) {

    try {
        return ResourceUtils.getFile("classpath:static/reports/"+file+".jasper");
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    };
    return null;
}

public byte[] generatePDFReport(String inputFileName, Map<String, Object> params,
        JRDataSource dataSource) {
        byte[] bytes = null;
        JasperReport jasperReport = null;
        try (ByteArrayOutputStream byteArray = new ByteArrayOutputStream()) {
          // Check if a compiled report exists


            jasperReport = (JasperReport) JRLoader.loadObject(loadJasperFile(inputFileName));

          // Compile report from source and save

          JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, dataSource);
          // return the PDF in bytes
          bytes = JasperExportManager.exportReportToPdf(jasperPrint);
        }
        catch (JRException | IOException e) {
          e.printStackTrace();
        }
        return bytes;
      }

}

my report is as below

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- Created with Jaspersoft Studio version 7.2.0.final using JasperReports Library version 6.6.0  -->
    <jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="refClient" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" isIgnorePagination="true" uuid="576dcd38-2982-412b-93d5-2a078da1b183">
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
        <style name="Table_TH" mode="Opaque" backcolor="#F0F8FF">
            <box>
                <pen lineWidth="0.5" lineColor="#000000"/>
                <topPen lineWidth="0.5" lineColor="#000000"/>
                <leftPen lineWidth="0.5" lineColor="#000000"/>
                <bottomPen lineWidth="0.5" lineColor="#000000"/>
                <rightPen lineWidth="0.5" lineColor="#000000"/>
            </box>
        </style>
        <style name="Table_CH" mode="Opaque" backcolor="#BFE1FF">
            <box>
                <pen lineWidth="0.5" lineColor="#000000"/>
                <topPen lineWidth="0.5" lineColor="#000000"/>
                <leftPen lineWidth="0.5" lineColor="#000000"/>
                <bottomPen lineWidth="0.5" lineColor="#000000"/>
                <rightPen lineWidth="0.5" lineColor="#000000"/>
            </box>
        </style>
        <style name="Table_TD" mode="Opaque" backcolor="#FFFFFF">
            <box>
                <pen lineWidth="0.5" lineColor="#000000"/>
                <topPen lineWidth="0.5" lineColor="#000000"/>
                <leftPen lineWidth="0.5" lineColor="#000000"/>
                <bottomPen lineWidth="0.5" lineColor="#000000"/>
                <rightPen lineWidth="0.5" lineColor="#000000"/>
            </box>
        </style>
        <style name="Table 1_TH" mode="Opaque" backcolor="#F0F8FF">
            <box>
                <pen lineWidth="0.5" lineColor="#000000"/>
                <topPen lineWidth="0.5" lineColor="#000000"/>
                <leftPen lineWidth="0.5" lineColor="#000000"/>
                <bottomPen lineWidth="0.5" lineColor="#000000"/>
                <rightPen lineWidth="0.5" lineColor="#000000"/>
            </box>
        </style>
        <style name="Table 1_CH" mode="Opaque" backcolor="#BFE1FF">
            <box>
                <pen lineWidth="0.5" lineColor="#000000"/>
                <topPen lineWidth="0.5" lineColor="#000000"/>
                <leftPen lineWidth="0.5" lineColor="#000000"/>
                <bottomPen lineWidth="0.5" lineColor="#000000"/>
                <rightPen lineWidth="0.5" lineColor="#000000"/>
            </box>
        </style>
        <style name="Table 1_TD" mode="Opaque" backcolor="#FFFFFF">
            <box>
                <pen lineWidth="0.5" lineColor="#000000"/>
                <topPen lineWidth="0.5" lineColor="#000000"/>
                <leftPen lineWidth="0.5" lineColor="#000000"/>
                <bottomPen lineWidth="0.5" lineColor="#000000"/>
                <rightPen lineWidth="0.5" lineColor="#000000"/>
            </box>
        </style>
        <subDataset name="ItemDataset" uuid="2914f7c6-c2d7-448b-b0c1-090970e18ed6">
            <queryString>
                <![CDATA[]]>
            </queryString>
            <field name="clientName" class="java.lang.String"/>
            <field name="identifiant" class="java.lang.String"/>
            <field name="codeExterne" class="java.lang.String"/>
        </subDataset>
        <parameter name="ItemDataSource" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
        <queryString>
            <![CDATA[select 1 from dual]]>
        </queryString>
        <background>
            <band splitType="Stretch"/>
        </background>
        <title>
            <band height="50">
                <staticText>
                    <reportElement x="11" y="11" width="100" height="30" uuid="9d66bc1e-2b6d-49b7-8a64-74fbd2e5d5eb"/>
                    <text><![CDATA[Liste des clients]]></text>
                </staticText>
            </band>
        </title>
        <detail>
            <band height="256" splitType="Stretch">
                <componentElement>
                    <reportElement x="0" y="0" width="555" height="200" uuid="7786bb05-503a-4b6a-991a-a7bcf3abad07">
                        <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
                        <property name="com.jaspersoft.studio.table.style.table_header" value="Table 1_TH"/>
                        <property name="com.jaspersoft.studio.table.style.column_header" value="Table 1_CH"/>
                        <property name="com.jaspersoft.studio.table.style.detail" value="Table 1_TD"/>
                    </reportElement>
                    <jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
                        <datasetRun subDataset="ItemDataset" uuid="d3e1e03a-2c83-4436-9e50-79518bc3d337">
                            <dataSourceExpression><![CDATA[$P{ItemDataSource}]]></dataSourceExpression>
                        </datasetRun>
                        <jr:column width="130" uuid="dca8961f-ce2d-47ac-879c-b15847d170a9">
                            <property name="com.jaspersoft.studio.components.table.model.column.name" value="Colonne1"/>
                            <jr:tableHeader style="Table 1_TH" height="30" rowSpan="1">
                                <staticText>
                                    <reportElement x="0" y="0" width="130" height="30" uuid="f60d1675-6fb6-4569-93ca-de32b3a8e861"/>
                                    <box>
                                        <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                        <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                        <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                        <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                    </box>
                                    <text><![CDATA[Nom]]></text>
                                </staticText>
                            </jr:tableHeader>
                            <jr:detailCell style="Table 1_TD" height="30">
                                <textField>
                                    <reportElement x="0" y="0" width="130" height="30" uuid="f1de366a-b36f-4200-9b1e-7a0009be5373"/>
                                    <box>
                                        <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                        <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                        <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                        <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                    </box>
                                    <textFieldExpression><![CDATA[$F{clientName}]]></textFieldExpression>
                                </textField>
                            </jr:detailCell>
                        </jr:column>
                        <jr:column width="140" uuid="980fffb5-1088-47a6-b00f-b5d08b7093c9">
                            <property name="com.jaspersoft.studio.components.table.model.column.name" value="Colonne2"/>
                            <jr:tableHeader style="Table 1_TH" height="30" rowSpan="1">
                                <staticText>
                                    <reportElement x="0" y="0" width="140" height="30" uuid="16679b95-2e6e-4ca1-b8b6-dd7b74267c56"/>
                                    <box>
                                        <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                        <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                        <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                        <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                    </box>
                                    <text><![CDATA[Identifiant]]></text>
                                </staticText>
                            </jr:tableHeader>
                            <jr:detailCell style="Table 1_TD" height="30">
                                <textField>
                                    <reportElement x="0" y="0" width="140" height="30" uuid="341e7faf-9680-4941-a9a4-ac78b8f4aae0"/>
                                    <box>
                                        <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                        <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                        <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                        <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                    </box>
                                    <textFieldExpression><![CDATA[$F{identifiant}]]></textFieldExpression>
                                </textField>
                            </jr:detailCell>
                        </jr:column>
                        <jr:column width="100" uuid="761d1134-6b53-4b5d-9355-b44586d8d8aa">
                            <property name="com.jaspersoft.studio.components.table.model.column.name" value="Colonne3"/>
                            <jr:tableHeader style="Table 1_TH" height="30" rowSpan="1">
                                <staticText>
                                    <reportElement x="0" y="0" width="100" height="30" uuid="b07f5db5-1df8-48e9-a612-27ad6110fdcd"/>
                                    <box>
                                        <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                        <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                        <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                        <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                    </box>
                                    <text><![CDATA[Code]]></text>
                                </staticText>
                            </jr:tableHeader>
                            <jr:detailCell style="Table 1_TD" height="30">
                                <textField>
                                    <reportElement x="0" y="0" width="100" height="30" uuid="1e0438b1-82e5-429a-b63a-5349014cf84a"/>
                                    <box>
                                        <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                        <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                        <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                        <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                    </box>
                                    <textFieldExpression><![CDATA[$F{codeExterne}]]></textFieldExpression>
                                </textField>
                            </jr:detailCell>
                        </jr:column>
                    </jr:table>
                </componentElement>
            </band>
        </detail>
    </jasperReport>

as you see I m using a parameters where I send a jrcollection, this is used in a tutorial that I found on youtube.

for the response handling I have this js ajax call

$(document).on('click', '#menu0-func1-menu0-func1', function(){
        console.log(printData);
        var jsonData =JSON.parse(JSON.stringify(printData));
            var settings = {
                "async" : true,
                "crossDomain" : true,
                "url" : "http://"+document.location.host+"/facturation/print/client",
                "method" : "POST",
                "headers" : {
                    "cache-control" : "no-cache",
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                "processData" : false,
                "contentType" : "application/json",
                "dataType" : "text",
                "data" : JSON.stringify(printData)
            }

            $.ajax(settings).done(function(response, status, xhr) {
                console.log(response);

                // check for a filename
                var filename = "";
                var disposition = xhr.getResponseHeader('Content-Disposition');
                if (disposition && disposition.indexOf('attachment') !== -1) {
                    var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
                    var matches = filenameRegex.exec(disposition);
                    if (matches != null && matches[1]) filename = matches[1].replace(/['"]/g, '');
                }

                var type = xhr.getResponseHeader('Content-Type');
                var blob = new Blob([response], { type: type });

                if (typeof window.navigator.msSaveBlob !== 'undefined') {
                    // IE workaround for "HTML7007: One or more blob URLs were revoked by closing the blob for which they were created. These URLs will no longer resolve as the data backing the URL has been freed."
                    window.navigator.msSaveBlob(blob, filename);
                } else {
                    var URL = window.URL || window.webkitURL;
                    var downloadUrl = URL.createObjectURL(blob);

                    if (filename) {
                        // use HTML5 a[download] attribute to specify filename
                        var a = document.createElement("a");
                        // safari doesn't support this yet
                        if (typeof a.download === 'undefined') {
                            window.location = downloadUrl;
                        } else {
                            a.href = downloadUrl;
                            a.download = filename;
                            document.body.appendChild(a);
                            a.click();
                        }
                    } else {
                        window.location = downloadUrl;
                    }

                    setTimeout(function () { URL.revokeObjectURL(downloadUrl); }, 100); // cleanup
                }

            });
    });

there is no exception, Normally I should at least get the title of the report but I m getting an empty pdf page

here is my console log in chrome

    %PDF-1.5
    %����
    3 0 obj
    <</Filter/FlateDecode/Length 882>>stream
    x���[O1���W��>�z|�M^i�J�B�T��e�Q��`����zJc�����}|2_���hU�R,d)Vuq�*�J�u!�W�{a�X�w' @�US�Y�t��D��ޤ�)�e��Ӧ8��m�ע^���n��n�#���9��O�\�ג�K��Rֺw�����
    �r��59��>/� 0^���r�`ޙ�ֈ�h�!�`�/���5�KƆ��Yb�\
    #��1hP�
    �ďK.fYyk?���%�6T�!�}�˾(j�on7����M���?F6���V��F�M^&�IjywI�h��������WV1d���L2N�w��̦%�E�c�B��I��єB�R��b!�2�f���d=*�I�����"�KB�"
    ��᳍&�������ֲX�df2��C"�zf�#)Ό�wLJ��F����аH��E#
    ��%/!b=��,�"y�XKyA˿��e
    endstream
    endobj
    1 0 obj
    <</Tabs/S/Group<</S/Transparency/Type/Group/CS/DeviceRGB>>/Contents 3 0 R/Type/Page/Resources<</ColorSpace<</CS/DeviceRGB>>/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]/Font<</F1 2 0 R>>>>/Parent 4 0 R/MediaBox[0 0 595 842]>>
    endobj
    5 0 obj
    [1 0 R/XYZ 0 852 0]
    endobj
    2 0 obj
    <</Subtype/Type1/Type/Font/BaseFont/Helvetica/Encoding/WinAnsiEncoding>>
    endobj
    4 0 obj
    <</Kids[1 0 R]/Type/Pages/Count 1/ITXT(2.1.7)>>
    endobj
    6 0 obj
    <</Names[(JR_PAGE_ANCHOR_0_1) 5 0 R]>>
    endobj
    7 0 obj
    <</Dests 6 0 R>>
    endobj
    8 0 obj
    <</Names 7 0 R/Type/Catalog/Pages 4 0 R/ViewerPreferences<</PrintScaling/AppDefault>>>>
    endobj
    9 0 obj
    <</ModDate(D:20181123161409Z)/Creator(JasperReports Library version 6.4.0)/CreationDate(D:20181123161409Z)/Producer(iText 2.1.7 by 1T3XT)>>
    endobj
    xref
    0 10
    0000000000 65535 f 
    0000000964 00000 n 
    0000001240 00000 n 
    0000000015 00000 n 
    0000001328 00000 n 
    0000001205 00000 n 
    0000001391 00000 n 
    0000001445 00000 n 
    0000001477 00000 n 
    0000001580 00000 n 
    trailer
    <</Info 9 0 R/ID [<b7d27a6f96e6278f5c1d222f79d7c999><b97ae89e1e568703aeb75af2080d04d8>]/Root 8 0 R/Size 10>>
    startxref
    1735
    %%EOF

What to do, I tried lot of tutorials this 3 last days but no success

edit

I generated the pdf using JasperExportManager.exportReportToPdfFile(jasperPrint, "rapport.pdf"); and the pdf file is well formed that is there, I compared the txt representation of this well pdf with the received log, they are the same, my empty pdf is 3kb and the well formed is also 3kb.


Answer:

I have tried your javascript code and there's an issue indeed.

But the issue comes from within jQuery's ajax implementation. It does not properly handle the response data type.

This post here presents two alternatives: either use the XMLHttpRequest object directly or use a custom jQuery plugin designed specifically for this issue.

Another option could be to split your controller logic in two parts:

  • on the first request you would fill in the report parameters
  • on the second one you would produce the PDF output; you could even have this opened in a new tab and skip the whole AJAX part.

Question:


Answer:

You are mixing the download of the PDF and the rendering of Thymeleaf.

Instead you should return a ResponseEntity with a byte[] that contains the PDF:

@Controller 
@RequestMapping("/report") 
public class RaporController { 

    @GetMapping() 
    public ResponseEntity reporting(Model model, HttpServletResponse response) throws IOException { 
        JasperPrint jasperPrint = reportService.report(new Date(), sqlTable, false); 
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        JasperExportManager.exportReportToPdfStream(jasperPrint, out);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_PDF);
        headers.setContentDispositionFormData(reportService.getFilename(), reportService.getFilename());
        headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");

        return new ResponseEntity(out.toByteArray(), headers, HttpStatus.OK);
     }
}

Question:

I am getting a strange error when trying to print my report.

Handler processing failed; nested exception is java.lang.VerifyError: class org.codehaus.groovy.runtime.callsite.GroovySunClassLoader$2 overrides final method visit.(IILjava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;)V

Please can anyone suggest what can be the solution for this.

here is the code for printing the jasper report.

@RequestMapping("/report")
    public String displayReport(Map<String,Object> map){

        System.out.println("Hello");
        String sourceFileName = "D://jasper"
                + "/report2.jrxml";
        String jasperFileName = "D://jasper"
                + "/report2.jasper";

        List<Project> projectList=projectService.getAllproject();
        String printFileName = null;
        JRBeanCollectionDataSource beanColDataSource =
                new JRBeanCollectionDataSource(projectList);

        try{

            // 1. Add report parameters
            HashMap<String, Object> parameters= new HashMap<String, Object>(); 

            parameters.put("ReportTitle", "Project Details");
            parameters.put("Author", "Prepared By Priyanka");

            JasperCompileManager.compileReportToFile(sourceFileName);
            printFileName = JasperFillManager.fillReportToFile(jasperFileName,
                    parameters, beanColDataSource);
            if (printFileName != null) {
                /**
                 * 1- export to PDF
                 */
                JasperExportManager.exportReportToPdfFile(printFileName,
                        "D://jasper/sample_report.pdf");

                /**
                 * 2- export to HTML
                 */
                JasperExportManager.exportReportToHtmlFile(printFileName,
                        "D://jasper/sample_report.html");

                /**
                 * 3- export to Excel sheet
                 */
                JRXlsExporter exporter = new JRXlsExporter();

                exporter.setParameter(JRExporterParameter.INPUT_FILE_NAME,
                        printFileName);
                exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,
                        "D://jasper/sample_report.xls");

                exporter.exportReport();
            }
        } catch (JRException e) {
            e.printStackTrace();
        }


        return "report"; 


    }

also the stack trace is as follows:

SEVERE: Servlet.service() for servlet [spring] in context with path [/Project5] threw exception [Handler processing failed; nested exception is java.lang.VerifyError: class org.codehaus.groovy.runtime.callsite.GroovySunClassLoader$2 overrides final method visit.(IILjava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;)V] with root cause java.lang.VerifyError: class org.codehaus.groovy.runtime.callsite.GroovySunClassLoader$2 overrides final method visit.(IILjava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;)V at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(Unknown Source) at java.security.SecureClassLoader.defineClass(Unknown Source) at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2895) at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1173) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1681) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559) at org.codehaus.groovy.runtime.callsite.CallSiteGenerator.isCompilable(CallSiteGenerator.java:243) at org.codehaus.groovy.reflection.CachedMethod.createPojoMetaMethodSite(CachedMethod.java:239) at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.createCachedMethodSite(PojoMetaMethodSite.java:158) at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.createPojoMetaMethodSite(PojoMetaMethodSite.java:147) at groovy.lang.MetaClassImpl.createPojoCallSite(MetaClassImpl.java:2979) at org.codehaus.groovy.runtime.callsite.CallSiteArray.createPojoSite(CallSiteArray.java:114) at org.codehaus.groovy.runtime.callsite.CallSiteArray.createCallSite(CallSiteArray.java:148) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:121) at org.codehaus.groovy.ast.builder.AstBuilderTransformation.visit(AstBuilderTransformation.groovy:61) at org.codehaus.groovy.transform.ASTTransformationVisitor$3.call(ASTTransformationVisitor.java:268) at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:799) at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:464) at net.sf.jasperreports.compilers.JRGroovyCompiler.compileUnits(JRGroovyCompiler.java:109) at net.sf.jasperreports.engine.design.JRAbstractCompiler.compileReport(JRAbstractCompiler.java:201) at net.sf.jasperreports.engine.JasperCompileManager.compile(JasperCompileManager.java:354) at net.sf.jasperreports.engine.JasperCompileManager.compileToFile(JasperCompileManager.java:270) at net.sf.jasperreports.engine.JasperCompileManager.compileToFile(JasperCompileManager.java:229) at net.sf.jasperreports.engine.JasperCompileManager.compileReportToFile(JasperCompileManager.java:539) at com.mvc.project.controller.ProjectController.displayReport(ProjectController.java:224) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176) at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426) at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549) at javax.servlet.http.HttpServlet.service(HttpServlet.java:621) at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1008) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source)


Answer:

The language attribute in the header of your report2.jrxml file is set to groovy, not java.

<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" 
... 
language="groovy" 
...>

Change this to

<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" 
... 
language="java" 
...>

Question:

We have a spring boot web application that we already using in prod. Recently the business asked to produce some EOD reconciliation reports as non editable PDFs.

We compiled some Jasper reports and start generating the reports using some code like below:

private void generatePdfReport(final Report report) throws SQLException, JRException {
    final JasperReport jasperReport = JASPER_REPORT_MAP.get(report);
    if (jasperReport == null) {
        throw new UnsupportedOperationException("The report is not supported: " + report.getName());
    } else {
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
            final JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, dataSource.getConnection());
            final JRPdfExporter exporter = new JRPdfExporter();
            exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
            exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(report.getFormattedFilename() + Report.EXT_PDF));
            exporter.setConfiguration(reportConfig);
            exporter.setConfiguration(exportConfig);
            exporter.exportReport();
            log.info("PDF Report ({}) exported successfully!", report.getName());
        } finally {
            if (connection != null) {
                log.info("about to close db connection");
                connection.close();
                connection = null;
            }
        }
    }
}

The reports run fine but after producing ten reports we started getting timeout exceptions from Hitaki data source complaining it could not acquire a connection in less than 30 seconds.

The default connection pool size for Hitaki data source is ten and putting some breakpoints in the Hitaki classes we could see that all ten connections are marked as IN_USE. Changing the data source to commons-dbcp did not change much. No timeouts this time but the processing blocked indefinitely after generating eight reports. Eight is the default pool size for dbcp data source.

Concluding it was not a data source problem we put some break points in the jasper jars and noticed that both result set and prepared statements created were properly closed.

Our Hitaki/DBCP datasources are wrapped by a spring TransactionAwareDataSourceProxy instance. Taking out that wrapping changed nothing.

In the end I replaced the datasource with a very basic data source (obviously not suitable for production) and all worked OK. See the code below showing as commented what we tried:

    @Bean
    public DataSource dataSource() {
        final String url = env.getProperty("database.url");
        final String userName = env.getProperty("gmm.schema");
        log.info("Creating DataSource for {}@{}", userName, url);
        // final HikariDataSource dataSource = new HikariDataSource();
        // final BasicDataSource dataSource = new BasicDataSource();
        final SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
        dataSource.setUrl(url);
        dataSource.setUsername(userName);
        dataSource.setPassword(env.getProperty("gmm.password"));
        // dataSource.setDriverClassName(env.getProperty("database.driver"));
       dataSource.setDriverClass(oracle.jdbc.driver.OracleDriver.class);
        return dataSource;
        // return new TransactionAwareDataSourceProxy(dataSource);
    }

Now my question:

What would cause such behavior and how should I fix or investigate further. There are no connections problems if we comment out the reporting job but on the other hand I don't see how using a very basic data source will fix this.

Being new to Jasper Reports I just hope I am not properly using it.

Thank you in advance.


Answer:

Try to Pass the connection object you created to fillReport method.

connection = dataSource.getConnection();
final JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, dataSource.getConnection());

to

connection = dataSource.getConnection();
        final JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, connection);

Cheers