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 Userentity 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 Roleentity 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:
In the service layer, code the UserServicesclass 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:
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:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
I need the source code in a zip for this example:- Spring Boot Export Data to PDF Example. I request you to send the source code of above example in a zip file to my email id:- sankaranarayanan102gmail.com.
I followed and exported to file but got UTF-8 font error even though I set font "Font font = FontFactory.getFont(FontFactory.TIMES_ROMAN, StandardCharsets.UTF_8.name(), 20);"
Comments