- Saved searches
- Use saved searches to filter your results more quickly
- License
- sergiodurand/jasper-examples
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
- Java | Simple Jasper Report Example
- Environment, Tools & Libraries used in this post
- Overview
- Dependencies
- Java Bean Country.java
- Java App.java
- JasperReports with Spring Boot
- Thank you!
- 1.1 JasperReports
- 2. JasperReports with Spring Boot
- 2.1 Tools Used
- 2.2 Project Structure
- 3. Creating a Springboot application
- 3.1 Maven Dependency
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Simple Java project with JasperReports example
License
sergiodurand/jasper-examples
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
Project on how to generate PDF report using JasperReports library
This project is based on Spring Boot and Maven with «batteries included»: all the necessary resources (library dependencies, database and jasper reports) are included. To play with this project you just need to clone it, call the maven wrapper and (if everything goes fine) open the PDF generated report.
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.0.6.RELEASE)
If you just want to run this example, you don’t need any IDE (Eclipse/Netbeans/IntelliJ). Assuming you are running Linux, you just need these lines:
$ git clone https://github.com/sergiodurand/jasper-examples.git $ cd jasper-examples $ ./mvnw spring-boot:run $ xdg-open /tmp/report.pdf
For this project I created a very simple SQLite database using the SQLite Browser with only one table and some rows with random values. The database file is clients.db and can be found at root of this project.
The database has the following structure:
Table: CLIENTS
Name | Type | Constraints |
---|---|---|
ID | INTEGER | PK |
NAME | TEXT | NOT NULL |
COUNTRY | TEXT | NOT NULL |
If you Google for JasperReports you will find that exist several ways to work with it. The approach I chose here is to use a JavaBean to represent the data of each report. I also chose to use sub-report approach just to have an example on how to deal with it.
Both source and compiled reports are available at resources directory. I have used the Jaspersoft Studio v6.6.0 to create and compile the reports.
I tried to keep the project as simple as possible.
The pom.xml file will take care of all dependencies used on this project like Spring Boot, SQLite and Jasper.
First the application access the Client service to retrieve the list of clients ordering by name and another one grouping by country.
Then it created a JasperReport data source object and they are added to the parameters map.
The final part of the application it to generate the report itself. I call the feelReport method passing the main report and the parameters. The main report will call the two sub-reports. Then it is called the exportReport to generate the PDF.
You can find the generated PDF at resources directory.
About
Simple Java project with JasperReports example
Java | Simple Jasper Report Example
JasperReports library is an open source reporting library written in Java. The library can export documents in different formats e.g. PDF, HTML, CSV…etc. This post shows a simple example of how to export PDF document using JasperReports.
Environment, Tools & Libraries used in this post
- Maven (build tool)
- jasperreports 6.10.0
- spring core 5.2.3.RELEASE
Overview
In order to generate a report we need to following inputs to be passed to Jasper exporting functions:
- Jasper Report Template: Jasper template is an XML .jrxml file that can be created using JasperSoft Studio.
- Parameters: This is a Java Map object containing a set of values passed from the application requesting the report. They can be used for runtime configuration or to pass additional custom data that is not part of the data source.
- Datasource: JasperReport engines expect an object of type JRDataSource as a data source. There several implementations of JRDataSource that allow to fetch data from different data source types such as databases, XML file, CSV file or Java beans.
In this post will export a PDF file using a List of Java bean Country as a data source. The template we are going to use report.jrxml is created using JasperSoft Studio.
Dependencies
net.sf.jasperreports jasperreports 6.10.0 org.springframework spring-core 5.2.3.RELEASE
Java Bean Country.java
- We have one simple Java class Country that holds country’s code, name and URL to an image of the contry’s flag.
public class Country private String code; private String name; private String url; public Country(String code, String name, String url) this.code = code; this.name = name; this.url = url; > . >
Java App.java
- The App.java class contains the main() method.
- The main() method call 3 custom methods
- getJasperReport() read & compiles .jrxml file.
- getParameters() returns the set of parameters.
- getDataSource() builds the data source .
package com.hmkcode; import net.sf.jasperreports.engine.*; import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource; import org.springframework.util.ResourceUtils; import java.io.*; import java.util.*; public class App // name and destination of output file e.g. "report.pdf" private static String destFileName = "report.pdf"; public static void main( String[] args ) throws FileNotFoundException, JRException System.out.println( "generating jasper report. " ); // 1. compile template ".jrxml" file JasperReport jasperReport = getJasperReport(); // 2. parameters "empty" MapString, Object> parameters = getParameters(); // 3. datasource "java object" JRDataSource dataSource = getDataSource(); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource); JasperExportManager.exportReportToPdfFile(jasperPrint, destFileName); > private static JasperReport getJasperReport() throws FileNotFoundException, JRException File template = ResourceUtils.getFile("classpath:report.jrxml"); return JasperCompileManager.compileReport(template.getAbsolutePath()); > private static MapString, Object> getParameters() MapString, Object> parameters = new HashMap<>(); parameters.put("createdBy", "hmkcode"); return parameters; > private static JRDataSource getDataSource() ListCountry> countries = new LinkedList<>(); countries.add(new Country("IS", "Iceland", "https://i.pinimg.com/originals/72/b4/49/72b44927f220151547493e528a332173.png")); // 9 other countries in GITHUB return new JRBeanCollectionDataSource(countries); > >
JasperReports with Spring Boot
In order to help you master the leading and innovative Java framework, we have compiled a kick-ass guide with all its major features and use cases! Besides studying them online you may download the eBook in PDF format!
Thank you!
1.1 JasperReports
JasperReports is an open-source reporting library. It is used to prepare reports in various formats such as PDF, HTML, XLS or CSV’s. This library creates page-oriented ready-to-print documents in a simple manner. The following flow chart illustrates the creation of reports.
In this tutorial, we will create a spring boot application that loads the sample employee data and creates a pdf report using the jasper-report library. To follow this concept, let us open the eclipse ide and implement this tutorial. But before going any further I’m assuming that readers are aware of the concepts of creating and running a basic spring boot application.
2. JasperReports with Spring Boot
Here is a systematic guide for implementing this tutorial.
2.1 Tools Used
We are using Eclipse Kepler SR2, JDK 8, and Maven.
2.2 Project Structure
In case you’re confused about where you should create the corresponding files or folder, let us review the project structure of the spring boot application.
Let us start building the application!
3. Creating a Springboot application
Below are the steps involved in developing the application.
3.1 Maven Dependency
Here, we specify the dependency for the spring boot, free marker, and jasper-reports. Maven will automatically resolve the other dependencies. The updated file will have the following code.