Jasper Reporting | Java

  1. Quickly look at the components we need.
  2. Build simple .jrxml file with jaspersoft studio
  3. Saving reports on backend
  4. View downloadable reports through web browser.
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.4.0</version>
</dependency>
<!-- http://mvnrepository.com/artifact/net.sf.jasperreports/jasperreports-fonts -->
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports-fonts</artifactId>
<version>6.0.0</version>
</dependency>
xml format
preview
String REPORTNAME = "yearlybooksummary"
InputStream employeeReportStream = getClass().getResourceAsStream("/ReportTemplate.jrxml");
JasperReport jasperReport = JasperCompileManager.compileReport(employeeReportStream);JRSaver.saveObject(jasperReport, "ReportTemplate.jasper");
//new jasper collection of records(it holds entity list)
JRBeanCollectionDataSource jasperRecords = new JRBeanCollectionDataSource(records);
//parameters hold some data like title,date we can pass them to //report
Map<String, Object> parameters = new HashMap<>();
parameters.put("date",date.toString());
parameters.put("topic", reportName);

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters,jasperRecords);
HttpHeaders header = new HttpHeaders();
final byte[] data;
//exporting pdf
JRPdfExporter exporterPDF = new JRPdfExporter();

exporterPDF.setExporterInput(new SimpleExporterInput(jasperPrint));
exporterPDF.setExporterOutput(new SimpleOutputStreamExporterOutput(REPORTNAME + ".pdf"));

SimplePdfReportConfiguration reportConfigPDF = new SimplePdfReportConfiguration();
reportConfigPDF.setSizePageToContent(true);
reportConfigPDF.setForceLineBreakPolicy(false);

SimplePdfExporterConfiguration exportConfig = new SimplePdfExporterConfiguration();
exportConfig.setMetadataAuthor("Isuru");
exportConfig.setEncrypted(true);
exportConfig.setAllowedPermissionsHint("PRINTING");

exporterPDF.setConfiguration(reportConfigPDF);
exporterPDF.setConfiguration(exportConfig);
//saving file in the back end
exporterPDF.exportReport();
log.info("REPORT : PDF File sent to Report Folder");

//exporting data to web browser at the end we push it...
//check last code snippet
data = JasperExportManager.exportReportToPdf(jasperPrint);

header.setContentType(MediaType.APPLICATION_PDF);
header.set(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=" + REPORTNAME + "pdf");
header.setContentLength(data.length);

log.info("REPORT : " + REPORTNAME + " PDF File sent to web browser");
case "xlsx":
JRXlsxExporter exporterXLS = new JRXlsxExporter();

exporterXLS.setExporterInput(new SimpleExporterInput(jasperPrint));
exporterXLS.setExporterOutput(new SimpleOutputStreamExporterOutput(REPORTNAME + ".xlsx"));


SimpleXlsxReportConfiguration reportConfigXLS= new SimpleXlsxReportConfiguration();
reportConfigXLS.setSheetNames(new String[] { "YEARLY BOOK SUMMARY" });

exporterXLS.setConfiguration(reportConfigXLS);
//exporting excel fie in the backend.exporterXLS.exportReport();

log.info("REPORT : " + REPORTNAME + " EXCEL File sent to Report Folder");

//sending excel file to browser
JRXlsxExporter xlsxExporter = new JRXlsxExporter();
final byte[] rawBytes;

try(ByteArrayOutputStream xlsReport = new ByteArrayOutputStream()){
xlsxExporter.setExporterInput(new SimpleExporterInput(jasperPrint));
xlsxExporter.setExporterOutput(new SimpleOutputStreamExporterOutput(xlsReport));
xlsxExporter.exportReport();

rawBytes = xlsReport.toByteArray();
}
data= rawBytes;
header.setContentType(MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"));
header.set(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=" + REPORTNAME + ".xlsx");
header.setContentLength(data.length);

log.info("REPORT : " + REPORTNAME + " EXCEL File sent to web browser");

break;

case "csv":
//creating csv file
JRCsvExporter exporterCSV = new JRCsvExporter();

exporterCSV.setExporterInput(new SimpleExporterInput(jasperPrint));
exporterCSV.setExporterOutput(new SimpleWriterExporterOutput("Reports/" + REPORTNAME + ".csv"));

exporterCSV.exportReport();

log.info("REPORT : " + REPORTNAME + "CSV File sent to Report Folder");

//sending PDF to browser
data = JasperExportManager.exportReportToPdf(jasperPrint);

header.setContentType(MediaType.APPLICATION_PDF);
header.set(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=" + REPORTNAME + ".pdf");
header.setContentLength(data.length);

log.info("REPORT : " + REPORTNAME + " PDF File sent to web browser");
break;
default:
data = JasperExportManager.exportReportToPdf(jasperPrint);
header.setContentType(MediaType.APPLICATION_PDF);
header.set(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=" + REPORTNAME + ".pdf");
header.setContentLength(data.length);
log.info("REPORT : No File Created , " + REPORTNAME + " PDF sent to browser.");
break;
return new HttpEntity<byte[]>(data, header);

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store