JasperReports: How to call the report in jsp page
Asked Answered
L

6

10

I made one jasper report using iReport 3.7.4 version, now i have to use that or call that report in my java application where i am using servlets, jsp and struts framework, apache tomcat as server.

I want steps regarding how to call the jasper report with some example.

Leeleeann answered 19/9, 2010 at 17:12 Comment(0)
S
8
  1. Compile the report in iReport
  2. Place the compiled report on the classpath
  3. load it with

    JasperReport jasperReport = (JasperReport) JRLoader.loadObject(inputStream);
    
  4. Fill it with data. dataSource is the DataSource instance you have - for example a BeanCollectionDataSource

    JasperPrint jasperPrint = 
         JasperFillManager.fillReport(jasperReport, params, dataSource);
    
  5. Export it

    JRPdfExporter exporter = new JRPdfExporter();
    exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream);
    exporter.exportReport();
    
  6. The outputStream above may be either a response.getOutputStream() or a FileOutputStream(), depending on whether you want to send it to a client or you want to store it as a file. If you want to send it to the client, you'd have to send the Content-Disposition header, and some more, but that depends on the format you want to save to. In case you want to print on the client, it's quite a different question - you'd need some client-side code, an applet, for example.

Subvention answered 19/9, 2010 at 19:9 Comment(1)
After 6 years since this was posted JRExporterParameter.OUTPUT_STREAM has been deprecated, I have posted an answer to shows how this answer is achieved in current version.Seize
S
4

After 6 years @Bozho answer now (v5 and v6) contains deprecated code on point 5 JRExporterParameter.OUTPUT_STREAM, but I will try to improve the other points while I'm at it

  1. Load the report

    compiled version .jasper

    JasperReport jasperReport = (JasperReport) JRLoader.loadObject(inputStream);
    

    or the non compiled version .jrxml (slower since need to compile but feasible)

    JasperReport jasperReport = JasperCompileManager.compileReport("path/to/myReport.jrxml");
    
  2. Fill the report

    with nothing (datasource generated inside report or just static text)

    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params);
    

    with datasource:

    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, dataSource);
    

    with database connection (may the most common, sql executed inside report)

    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, connection);
    
  3. Export report

    JRPdfExporter exporter = new JRPdfExporter()
    exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
    exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputStream));
    SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
    configuration.setMetadataAuthor("Petter"); //Set your pdf configurations, 
    exporter.setConfiguration(configuration);
    exporter.exportReport();
    
  4. If you like to stream the report directly to web page this is how contentType and Content-disposition is set and how you retrieve the outputStream

    response.setContentType("application/x-pdf");
    response.setHeader("Content-disposition", "inline; filename=myReport.pdf");
    OutputStream outputStream = response.getOutputStream();
    
Seize answered 29/2, 2016 at 11:22 Comment(0)
M
1

This piece of code should give you some idea on how to do it

JasperReport jr=JasperCompileManager.compileReport("yourJRXMLFilePath");
JasperPrint jrPrint = JasperFillManager.fillReport(jr,mapWithParameters,aJRDataSource);
JasperExportManager.chooseYourFavoriteMethod(jrPrint,"destinationFile");

Otherwise, check the api The first line can be ommited if you had already compiled the file with iReport. Check the api for the correct method on JasperFillManager in this case.

Margret answered 19/9, 2010 at 17:28 Comment(0)
G
1

in the first answer, point 5: After

JRPdfExporter exporter= new JRPdfExporter();

add

exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);

Gine answered 7/12, 2010 at 14:16 Comment(0)
P
1

Best solution (For better performance as well), will be calling a compiled report.

you can see the example below

import java.io.IOException;
import java.util.HashMap;

import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;

public class PdfFromJasperFile {
  public static void main(String[] args) throws JRException, IOException {

    JasperPrint jasperPrint = JasperFillManager.fillReport("report.jasper",  new HashMap<String, Object>(), 
    new JREmptyDataSource());
    JasperExportManager.exportReportToPdfFile(jasperPrint, "sample.pdf");

  }
}
Piscatelli answered 21/2, 2012 at 17:53 Comment(0)
P
0

This is a different way of doing the same.

    JasperReport jasperReport;
    JasperPrint jasperPrint;
    Map<String, Object> param = new HashMap<String, Object>();
    try{
        String sourceFileName = ".jrxml";
        jasperReport = JasperCompileManager.compileReport(sourceFileName);
        jasperPrint = JasperFillManager.fillReport(jasperReport,param,new JRBeanCollectionDataSource(getDetails()));
        JasperExportManager.exportReportToPdfFile(jasperPrint, ".pdf");
    }
    catch(Exception e){
    }
Polaroid answered 15/12, 2011 at 9:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.