Spring Boot Export Data to PDF Example
- Details
- Written by Nam Ha Minh
- Last Updated on 04 September 2020   |   Print Email
Through this article, you will learn how to implement PDF export functionality for a Spring Boot application with OpenPDF (LibrePDF) is the Java library chosen for generating PDF documents. The code examples below demonstrate this scenario: the users click Export to PDF link and then the application will retrieve the requested information from the database, and generate a PDF document to the browser which will download the PDF file.
Suppose that we have an existing Spring Boot application that makes use of Spring Data JPA and Hibernate for the data access layer, Thymeleaf for the view and MySQL as the database.
1. Code for the Data Access layer
We will develop a function that allows the users to export information about users from the database to a PDF document. So we have the User entity class that maps to the users table in the database, as shown below:
package net.codejava; import java.util.*; import javax.persistence.*; @Entity @Table(name = "users") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; private String email; private String password; @Column(name = "full_name") private String fullName; private boolean enabled; @ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER) @JoinTable( name = "users_roles", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id") ) private Set<Role> roles = new HashSet<>(); // constructors, getter and setters are not shown for brevity }
And the Role entity class that maps to the roles table in the database:
package net.codejava; import javax.persistence.*; @Entity @Table(name = "roles") public class Role { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; private String name; private String description; // constructors, getter and setters are not shown for brevity }
The fields will be included in the generated PDF document are: User ID, E-mail, Full Name, Roles and Enabled.
And code of the respective repository interfaces looks like this:
package net.codejava; import org.springframework.data.jpa.repository.JpaRepository; public interface UserRepository extends JpaRepository<User, Integer> { } public interface RoleRepository extends CrudRepository<Role, Integer> { }
These are simple, typical repositories as required by Spring Data JPA.
2. Declare Dependency for PDF Library
To generate a PDF document, we need to use an external Java PDF library such as OpenPDF. So declare the following dependency in the project’s pom.xml file:
<dependency> <groupId>com.github.librepdf</groupId> <artifactId>openpdf</artifactId> <version>1.3.8</version> </dependency>
3. Code for the Service layer
In the service layer, code the UserServices class as below
package net.codejava; import java.util.List; import javax.transaction.Transactional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; @Service @Transactional public class UserServices { @Autowired private UserRepository repo; public List<User> listAll() { return repo.findAll(Sort.by("email").ascending()); } }
As you can see, the listAll() method returns a List collection of User objects from the database. It delegates the call UserRepository’s findAll() method which will be implemented by Spring Data JPA at runtime.
4. Code PDF Exporter Class
Next, code a separate class that is responsible to generate a PDF document based on the input is a List collection of User objects, as shown below:
package net.codejava; import java.awt.Color; import java.io.IOException; import java.util.List; import javax.servlet.http.HttpServletResponse; import com.lowagie.text.*; import com.lowagie.text.pdf.*; public class UserPDFExporter { private List<User> listUsers; public UserPDFExporter(List<User> listUsers) { this.listUsers = listUsers; } private void writeTableHeader(PdfPTable table) { PdfPCell cell = new PdfPCell(); cell.setBackgroundColor(Color.BLUE); cell.setPadding(5); Font font = FontFactory.getFont(FontFactory.HELVETICA); font.setColor(Color.WHITE); cell.setPhrase(new Phrase("User ID", font)); table.addCell(cell); cell.setPhrase(new Phrase("E-mail", font)); table.addCell(cell); cell.setPhrase(new Phrase("Full Name", font)); table.addCell(cell); cell.setPhrase(new Phrase("Roles", font)); table.addCell(cell); cell.setPhrase(new Phrase("Enabled", font)); table.addCell(cell); } private void writeTableData(PdfPTable table) { for (User user : listUsers) { table.addCell(String.valueOf(user.getId())); table.addCell(user.getEmail()); table.addCell(user.getFullName()); table.addCell(user.getRoles().toString()); table.addCell(String.valueOf(user.isEnabled())); } } public void export(HttpServletResponse response) throws DocumentException, IOException { Document document = new Document(PageSize.A4); PdfWriter.getInstance(document, response.getOutputStream()); document.open(); Font font = FontFactory.getFont(FontFactory.HELVETICA_BOLD); font.setSize(18); font.setColor(Color.BLUE); Paragraph p = new Paragraph("List of Users", font); p.setAlignment(Paragraph.ALIGN_CENTER); document.add(p); PdfPTable table = new PdfPTable(5); table.setWidthPercentage(100f); table.setWidths(new float[] {1.5f, 3.5f, 3.0f, 3.0f, 1.5f}); table.setSpacingBefore(10); writeTableHeader(table); writeTableData(table); document.add(table); document.close(); } }
This class will create a PDF representing the information about users in table format, with table header consists of these columns: User ID, E-mail, Full Name, Roles and Enabled.
Pay attention to the export() method that takes an HttpServletRespone as the argument, because it will write the content of the PDF file into the output stream of the response, so the clients (web browsers) will be able to download the exported PDF document.
5. Code Handler method in the Controller Class
Next, implement a handler method in a Spring MVC controller class – UserController – as follows:
package net.codejava; import java.io.IOException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import com.lowagie.text.DocumentException; @Controller public class UserController { @Autowired private UserServices service; @GetMapping("/users/export/pdf") public void exportToPDF(HttpServletResponse response) throws DocumentException, IOException { response.setContentType("application/pdf"); DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss"); String currentDateTime = dateFormatter.format(new Date()); String headerKey = "Content-Disposition"; String headerValue = "attachment; filename=users_" + currentDateTime + ".pdf"; response.setHeader(headerKey, headerValue); List<User> listUsers = service.listAll(); UserPDFExporter exporter = new UserPDFExporter(listUsers); exporter.export(response); } }
As you can see, the exportToPDF() method will serve HTTP GET request with the URI /users/export/pdf. It will use the UserServices class to get data of users from the database, and use the UserPDFExporter class to write a PDF document to the response.
Also notice name of the generated PDF file is appended with the current date time, making it’s easier for the users to track multiple versions of files downloaded.
6. Add Export PDF Link in the View layer
We use HTML and Thymeleaf to create a hyperlink that allows the user to click to export data to PDF as follows:
<a th:href="/@{/users/export/pdf}">Export to PDF</a>
7. Test Export and Download PDF document
Click the hyperlink Export to PDF, the Spring Boot application will generate an PDF file and the browser will automatically download that file. The file name is something like this: users_2020-09-04_10_47_46.pdf. Open this file using a PDF reader application (or view right in the browser), you would see the following screen:
Conclusion
So far you have learned how to implement PDF export functionality for a Spring Boot web application. You see, Spring Data JPA makes it easy to get data from the database, and OpenPDF makes it easy to generate PDF documents.
For video version of this tutorial, watch the video below:
Related Tutorials:
Other Spring Boot Tutorials:
- Spring Boot Export Data to CSV Example
- Spring Boot Export Data to Excel Example
- Spring Boot Hello World Example
- Spring Boot automatic restart using Spring Boot DevTools
- Spring Boot Form Handling Tutorial with Spring Form Tags and JSP
- How to create a Spring Boot Web Application (Spring MVC with JSP/ThymeLeaf)
- Spring Boot - Spring Data JPA - MySQL Example
- Spring Boot Hello World RESTful Web Services Tutorial
- How to use JDBC with Spring Boot
- Spring Boot CRUD Web Application with JDBC - Thymeleaf - Oracle
- Spring Boot RESTful CRUD API Examples with MySQL database
- How to package Spring Boot application to JAR and WAR
- Spring Boot Security Authentication with JPA, Hibernate and MySQL
- Spring Data JPA Paging and Sorting Examples
- Spring Boot Error Handling Guide
Comments