Almost every client expects the report of data as it is in the database. The most popular & user-friendly reports are PDF & Excel. Here, in this article, we will learn how to generate dynamic PDF report. We will fetch all values from the database, no hard-coding will be done. Further to escape from hard-coding we will take some of the values from properties file. In this way we donโt need to modify the java file in case we have some change requests in future. Letโs get into the topic โHow to Generate Dynamic PDF Report using Spring Boot?โ.
Here we will use Spring Boot to generate our dynamic PDF. However, you can easily utilize the code from this example to generate PDF in any other framework of Java. Moreover, you can generate in a Simple Servlet application also using this spring boot pdf generation example.
What all functionalities will you get in generated PDF?
1) All contents in the PDF which are appearing as static, are actually dynamic. Moreover It is designed in such a way that you donโt need to make changes in your source code. You just have to provide entries of dynamic values in application.properties file accordingly.
2) You can easily replace the Logo on the upper right corner.
3) The header line of the PDF โEmployee-Reportโ is dynamic & can be changed.
4) Also, Number of columns in the PDF table can be updated as per your need.
5) You can easily replace the Name of Table columns as per your need.
6) Values in the PDF table are dynamic & taken from the database.
7) Moreover, any prefix can be added to update all values of a particular column. It is like โ$โ symbol as a prefix in the โEmp Salโ column.
8) We can also update PDF footer easily.
9) The next important thing, We can have the PDF file name suffixed or prefixed with the current date.
What will you learn after implementing this application?
1) Where to use Annotations such as @Value, @Query, @Autowired, @Component, @Entity, @Id etc. ?
2) How to work with application.properties file?
3) In addition, How to inject values from properties file as an Array & List?
4) How to write code without hard-coding the values using Spring Boot?
5) Also, How to use List.Of() method introduced in JDK9?
6) How to include any prefix dynamically in all values of a particular column?
7) Equally important, How to write modular & reusable code?
8) How to implement dynamic code with minimal changes, keeping future change requests in mind?
9) Last but not the least you will learn โHow to Generate Dynamic PDF Report using Spring Boot?โ.
Software Used in this project?
โฆ STS (Spring Tool Suite) : Version-> 4.7.1.RELEASE
โ Dependent Starters : Spring Data JPA, MySQL Driver, Spring Configuration Processor
โฆ MySQL Database : Version ->8.0.19 MySQL Community Server
โฆ JDK8 or later versions (Extremely tested on JDK8, JDK9 and JDK14)
External Dependencies
In order to get the features of PDF in our example โHow to Generate Dynamic PDF Report using Spring Boot?โ, we will add following โiTextโ dependency in pom.xml as an external jar.
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
You can find this dependency from itext mvn repository.
Pre-requisites
Before implementing the example โHow to Generate Dynamic PDF Report using Spring Boot?โ, you should already have a database to fetch values from there. If you are not familiar with โhow to save data into databaseโ, you can visit to โSaving Data into database in a batch using Spring boot- Data JPAโ. You can also use your own existing database to test the functionalities accordingly.
Coding Steps
Step #1 : Create Project in STS
If you are new in Spring Boot, kindly visit Internal Link to create a sample project in spring boot.
Step #2 :ย Writing classes & updating application.properties
Package/Location | Class/Interface name | Create/Update |
---|---|---|
com.dev.springboot | SpringBoot2PdfGenerationApplication.java | update |
com.dev.springboot.model | Employee.java | create |
com.dev.springboot.repository | EmployeeRepository.java (interface) | create |
com.dev.springboot.util.pdf | PDFGenerator.java | create |
src/main/resources | application.properties | update |
Your project structure should look like below screenshot.
Below are the codes for each file.
application.properties -------------------------------------------------------------------------------------------------- #------------Database properties--------------------- spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/mytestdb spring.datasource.username=root spring.datasource.password=devs spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=update #------------PDF Genration properties--------------------- pdfDir=D:/PdfReportRepo/ reportFileName=Employee-Report reportFileNameDateFormat=dd_MMMM_yyyy localDateFormat="dd MMMM yyyy HH:mm:ss" logoImgPath=D:/img_JTO_logo.jpg logoImgScale=50,50 currencySymbol=$ table_noOfColumns=4 table.columnNames=Emp Id,Emp Name,Emp Dept,Emp Sal
How to run the application?
We have done with the implementation of our example โHow to Generate Dynamic PDF Report using Spring Boot?โ. Now, in order to run the application, right click on Project and select Run As >> Spring Boot App. Subsequently, observe the output in the console. You will see a message โโYour PDF Report is ready!โโโ
Testing Results
In order to test the results, go to the location where you have saved your generated PDF. Now verify the values you are expecting to appear in the PDF. Your PDF will look like below screenshot.
How to implement our own PDF Report from this Example with minimal changes?
We need to change values of properties included in the application.properties.
1) Update the values of the first 4 properties as per your database.
2) Then, Update the pdf file storage path in the value of property โpdfDirโ.
3) To change the Logo on the upper right corner change the values of 2 properties : โlogoImgPathโ & โlogoImgScaleโ. โlogoImgPathโ is forย ย ย image storage path in your system and โlogoImgScaleโ is for image scaling in percentage.
4) In order to change the Header of the PDF change the values of โreportFileNameโ.
5) To change the format of Date in the first line change the value of โlocalDateFormatโ.
6) Next, To change the Number of Columns in the PDF table change the value of โtable_noOfColumnsโ.
7) In order to change the Column Names of PDF table, change the values of โtable.columnNamesโ. In the end, make sure that number of column names values are equal to the value of โtable_noOfColumnsโ property. Otherwise, you will get an error.
8) To change the currency from โ$โ to any other symbol change the value of โcurrencySymbolโ. However If you donโt want to include any symbol leave it blank. Any other prefix can also be included with all values of a particular column.
Below are the changes in java files :
9) In order to display dynamic database values you need to create a new Entity class, new Repository class in place of Employee.java & EmployeeRepository.java respectively.
10) Subsequently, you need to update getDbData() method of PDFGenerator.java as per your database.
Can we apply this implementation in a real project ?
Of course, you can apply this implementation in a real project if no constraints on using itextpdf library. You just have to do modifications described above according to your requirement.
What are the changes do we need to do if we want to use this functionality in our real project ?
In order to use the implementation of โHow to Generate Dynamic PDF Report using Spring Boot?โ in your real project, you have to go through the minimal modifications suggested in the above section. That is โHow to implement our own PDF Report from this Example with minimal changes?โ of this article.
Conclusion
In this article we have covered all the theoretical and example part of โHow to Generate Dynamic PDF Report using Spring Boot?โ, finally, you should be able to implement a pdf generation using Spring Boot. Similarly, we expect from you to further extend this example, as per your requirement. Also, try to implement it in your project accordingly. Moreover, Feel free to provide your comments in the comments section below.
โฆ Additionally, If you face any issue on running this project, donโt hesitate to write your comments below.
Youโve made some good points there. I checked on the net for more info about the issue and found most people will go along with your views on this web site.