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.
To run a report on the server: 1. Once you have a connection to your server Connecting to JasperReports Server, navigate to your report's JRXML, and click Run Report Unit. If prompted to save the report unit, specify a location on your local computer and click OK.
load it with
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(inputStream);
Fill it with data. dataSource
is the DataSource
instance you have - for example a BeanCollectionDataSource
JasperPrint jasperPrint =
JasperFillManager.fillReport(jasperReport, params, dataSource);
Export it
JRPdfExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream);
exporter.exportReport();
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.
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
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");
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);
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();
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With