iText PDF + SpringBoot RestAPI – Extract MySQL Data to PDF File

spring-boot-spring-jpa-export-data-pdf-table-file-by-itext-mysql-feature-image

In the tutorial, we show you how to create a SpringBoot RestAPIs application that uses Spring JPA & iText library to extract data from MySQL records to a PDF file in table format.

Related posts:
How to use Spring JPA with PostgreSQL | Spring Boot

Technologies

– SpringBoot
– iText 5.0.6
– MySQL

Practice

Project Structure ->

spring-boot-spring-jpa-export-data-pdf-table-file-by-itext-mysql-project-structure

Create SpringBoot project

Use SpringToolSuite to create a SpringBoot project with below dependencies:

Customer Model

Customer.java ->

JPA Customer Repository

CustomerRepository.java ->

Implement PDF Generator

PDFGenerator.java ->

– Segment Code to add PDF text with iText:

– Segment code to create PDF table having 3 columns:

-> Create table’s headers:

-> Add table’s rows, each row has 3 cells that contains a Customer’s data: {id, firstName, lastName}:

Implement RestAPI Controller

CustomerRestAPIs ->

Initial Customers

– In main class, we use CommandLineRunner to init Customer’s records:

Database Configuration

application.properties ->

Run & Check Results

Run the SpringBoot project, results:

-> MySQL’s records:

spring-boot-spring-jpa-export-data-pdf-table-file-by-itext-mysql-records

-> PDF File:

spring-boot-spring-jpa-export-data-pdf-table-file-by-itext-mysql-results

SourceCode

SpringJpaPDFReport

By grokonez | October 15, 2018.



Related Posts


2 thoughts on “iText PDF + SpringBoot RestAPI – Extract MySQL Data to PDF File”

  1. 1. How can I make generated pdf file automatic download when I hit the api link(like your xls tutorial)
    2. How to make this as generic? For example I have different different objects like customer, user product and I want to make something generic to prepare pdf, xls and download?

    1. Hello, what you have to do is change only ine line of code, this change will let you open de pdf in one separated page and download de pdf:

      headers.add(“Content-Disposition”, “attachment; filename=customers.pdf”);

Got Something To Say:

Your email address will not be published. Required fields are marked *

*