Jasper Reports with Spring
Overview
Generate a pdf report using jasper reports
Github: https://github.com/gitorko/project70
Jasper Report
JasperReports is an open source java reporting engine. It can generate different types of reports in this example we look at generating a pdf report with data passed from the java layer. To generate the jasper template you will need to download and install jasper studio. Jasper report also comes with a server but for this demo you dont need to install it.
https://community.jaspersoft.com/download
Code
1package com.demo.project70;
2
3import java.io.File;
4import java.io.FileOutputStream;
5import java.io.OutputStream;
6import java.util.ArrayList;
7import java.util.HashMap;
8import java.util.List;
9import java.util.Map;
10
11import lombok.AllArgsConstructor;
12import lombok.Data;
13import lombok.NoArgsConstructor;
14import net.sf.jasperreports.engine.JREmptyDataSource;
15import net.sf.jasperreports.engine.JasperCompileManager;
16import net.sf.jasperreports.engine.JasperExportManager;
17import net.sf.jasperreports.engine.JasperFillManager;
18import net.sf.jasperreports.engine.JasperPrint;
19import net.sf.jasperreports.engine.JasperReport;
20import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
21import org.springframework.boot.CommandLineRunner;
22import org.springframework.boot.SpringApplication;
23import org.springframework.boot.autoconfigure.SpringBootApplication;
24import org.springframework.context.annotation.Bean;
25
26@SpringBootApplication
27public class Main {
28
29 static final String fileName = "src/main/resources/EmployeeReports.jrxml";
30 static final String outFile = "EmployeeReports.pdf";
31
32 public static void main(String[] args) {
33 SpringApplication.run(Main.class, args);
34 }
35
36 @Bean
37 public CommandLineRunner onStart() {
38 return (args) -> {
39 List<Employee> employeeList = new ArrayList<>();
40 Map<String, Object> parameter = new HashMap<>();
41
42 employeeList.add(new Employee(1, "Jack Ryan", 100.0));
43 employeeList.add(new Employee(2, "Cathy Mueller", 130.0));
44 employeeList.add(new Employee(3, "Matice", 90.0));
45
46 parameter.put("employeeDataSource", new JRBeanCollectionDataSource(employeeList));
47 parameter.put("title", "Employee Report");
48
49 JasperReport jasperDesign = JasperCompileManager.compileReport(fileName);
50 JasperPrint jasperPrint = JasperFillManager.fillReport(jasperDesign, parameter, new JREmptyDataSource());
51
52 File file = new File(outFile);
53 OutputStream outputSteam = new FileOutputStream(file);
54 JasperExportManager.exportReportToPdfStream(jasperPrint, outputSteam);
55
56 System.out.println("Report Generated!");
57 };
58 }
59
60 @Data
61 @AllArgsConstructor
62 @NoArgsConstructor
63 public class Employee {
64 private int id;
65 private String name;
66 private Double salary;
67 }
68}
69
70
1<?xml version="1.0" encoding="UTF-8"?>
2<!-- Created with Jaspersoft Studio version 6.14.0.final using JasperReports Library version 6.14.0-2ab0d8625be255bf609c78e1181801213e51db8f -->
3<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="EmployeeReports" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="78066d92-d5f8-4a86-bde2-9824be76fbdf">
4 <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
5 <style name="Table_TH" mode="Opaque" backcolor="#F0F8FF">
6 <box>
7 <pen lineWidth="0.5" lineColor="#000000"/>
8 <topPen lineWidth="0.5" lineColor="#000000"/>
9 <leftPen lineWidth="0.5" lineColor="#000000"/>
10 <bottomPen lineWidth="0.5" lineColor="#000000"/>
11 <rightPen lineWidth="0.5" lineColor="#000000"/>
12 </box>
13 </style>
14 <style name="Table_CH" mode="Opaque" backcolor="#BFE1FF">
15 <box>
16 <pen lineWidth="0.5" lineColor="#000000"/>
17 <topPen lineWidth="0.5" lineColor="#000000"/>
18 <leftPen lineWidth="0.5" lineColor="#000000"/>
19 <bottomPen lineWidth="0.5" lineColor="#000000"/>
20 <rightPen lineWidth="0.5" lineColor="#000000"/>
21 </box>
22 </style>
23 <style name="Table_TD" mode="Opaque" backcolor="#FFFFFF">
24 <box>
25 <pen lineWidth="0.5" lineColor="#000000"/>
26 <topPen lineWidth="0.5" lineColor="#000000"/>
27 <leftPen lineWidth="0.5" lineColor="#000000"/>
28 <bottomPen lineWidth="0.5" lineColor="#000000"/>
29 <rightPen lineWidth="0.5" lineColor="#000000"/>
30 </box>
31 </style>
32 <subDataset name="employeeDataSet" uuid="9651295c-429d-420d-9e5b-42055e73efea">
33 <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
34 <queryString>
35 <![CDATA[]]>
36 </queryString>
37 <field name="id" class="java.lang.Integer"/>
38 <field name="name" class="java.lang.String"/>
39 <field name="salary" class="java.lang.Double"/>
40 </subDataset>
41 <parameter name="title" class="java.lang.String"/>
42 <parameter name="employeeDataSource" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
43 <queryString>
44 <![CDATA[]]>
45 </queryString>
46 <background>
47 <band splitType="Stretch"/>
48 </background>
49 <title>
50 <band height="79" splitType="Stretch">
51 <textField>
52 <reportElement x="250" y="20" width="100" height="30" uuid="2f6bf147-59e5-4468-84b4-bf89a58646ef"/>
53 <textElement>
54 <font size="12"/>
55 </textElement>
56 <textFieldExpression><![CDATA[$P{title}]]></textFieldExpression>
57 </textField>
58 </band>
59 </title>
60 <pageHeader>
61 <band height="35" splitType="Stretch"/>
62 </pageHeader>
63 <columnHeader>
64 <band height="61" splitType="Stretch"/>
65 </columnHeader>
66 <detail>
67 <band height="246" splitType="Stretch">
68 <componentElement>
69 <reportElement x="180" y="30" width="200" height="200" uuid="63d288f4-369c-494f-85b0-f0108b22765e">
70 <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
71 <property name="com.jaspersoft.studio.table.style.table_header" value="Table_TH"/>
72 <property name="com.jaspersoft.studio.table.style.column_header" value="Table_CH"/>
73 <property name="com.jaspersoft.studio.table.style.detail" value="Table_TD"/>
74 </reportElement>
75 <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">
76 <datasetRun subDataset="employeeDataSet" uuid="6fe4bdfe-9dd2-4d5e-b94b-c951044cc1cf">
77 <dataSourceExpression><![CDATA[$P{employeeDataSource}]]></dataSourceExpression>
78 </datasetRun>
79 <jr:column width="66" uuid="4ba6dc06-71ed-4eac-925f-3940726ecfa8">
80 <jr:tableHeader style="Table_TH" height="30"/>
81 <jr:tableFooter style="Table_TH" height="30"/>
82 <jr:columnHeader style="Table_CH" height="30">
83 <staticText>
84 <reportElement x="0" y="0" width="66" height="30" uuid="2fa1464e-80ba-4599-b563-c9f922460fc6"/>
85 <text><![CDATA[id]]></text>
86 </staticText>
87 </jr:columnHeader>
88 <jr:columnFooter style="Table_CH" height="30"/>
89 <jr:detailCell style="Table_TD" height="30">
90 <textField>
91 <reportElement x="0" y="0" width="66" height="30" uuid="fcae93e6-a2e8-44d2-b05a-18cf222b2580"/>
92 <textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
93 </textField>
94 </jr:detailCell>
95 </jr:column>
96 <jr:column width="66" uuid="a9abc185-068b-44b0-ba3e-c52793b91a90">
97 <jr:tableHeader style="Table_TH" height="30"/>
98 <jr:tableFooter style="Table_TH" height="30"/>
99 <jr:columnHeader style="Table_CH" height="30">
100 <staticText>
101 <reportElement x="0" y="0" width="66" height="30" uuid="02aa9397-a647-477d-ae35-d87a6d2c7bc1"/>
102 <text><![CDATA[name]]></text>
103 </staticText>
104 </jr:columnHeader>
105 <jr:columnFooter style="Table_CH" height="30"/>
106 <jr:detailCell style="Table_TD" height="30">
107 <textField>
108 <reportElement x="0" y="0" width="66" height="30" uuid="f13bc7e1-3206-4e65-9377-9e488acec741"/>
109 <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
110 </textField>
111 </jr:detailCell>
112 </jr:column>
113 <jr:column width="66" uuid="d0e58992-65cf-48e0-a03a-3fc71f88c1da">
114 <jr:tableHeader style="Table_TH" height="30"/>
115 <jr:tableFooter style="Table_TH" height="30"/>
116 <jr:columnHeader style="Table_CH" height="30">
117 <staticText>
118 <reportElement x="0" y="0" width="66" height="30" uuid="cb76f4a3-45a1-4b8c-9e41-e4e586c79496"/>
119 <text><![CDATA[salary]]></text>
120 </staticText>
121 </jr:columnHeader>
122 <jr:columnFooter style="Table_CH" height="30"/>
123 <jr:detailCell style="Table_TD" height="30">
124 <textField>
125 <reportElement x="0" y="0" width="66" height="30" uuid="76129bc3-7141-42f8-9059-3fb60b79ec4d"/>
126 <textFieldExpression><![CDATA[$F{salary}]]></textFieldExpression>
127 </textField>
128 </jr:detailCell>
129 </jr:column>
130 </jr:table>
131 </componentElement>
132 </band>
133 </detail>
134 <columnFooter>
135 <band height="45" splitType="Stretch"/>
136 </columnFooter>
137 <pageFooter>
138 <band height="54" splitType="Stretch"/>
139 </pageFooter>
140 <summary>
141 <band height="42" splitType="Stretch"/>
142 </summary>
143</jasperReport>
Run the project to generate the EmployeeReports.pdf file.
Setup
1# Project 70
2
3Jasper Report with Spring
4
5[https://gitorko.github.io/jasper-reports-spring/](https://gitorko.github.io/jasper-reports-spring/)
6
7### Version
8
9Check version
10
11```bash
12$java --version
13openjdk version "21.0.3" 2024-04-16 LTS
14```
15
16### Dev
17
18To generate the pdf report.
19
20```bash
21./gradlew clean build
22./gradlew bootRun
23```
To create the jasper report template file you can use jasper studio
References
comments powered by Disqus