DynamicPDFGenerator is a Spring Boot application designed to generate custom PDFs dynamically using client-provided data. It leverages Thymeleaf templates to create documents such as invoices or reports in real-time, with the PDFs returned directly through a REST API response. This tool is ideal for systems that require flexible, on-demand PDF creation.
DynamicPDFGenerator/
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ └── com/
│ │ │ └── freightfox/
│ │ │ └── pdfgenerator/
│ │ │ ├── config/
│ │ │ │ ├── AppConfig.java
│ │ │ │ ├── SwaggerConfig.java
│ │ │ │ └── WebConfig.java
│ │ │ ├── controller/
│ │ │ │ └── PdfController.java
│ │ │ ├── exception/
│ │ │ │ └── GlobalExceptionHandler.java
│ │ │ ├── model/
│ │ │ │ └── PdfRequest.java
│ │ │ ├── service/
│ │ │ │ └── PdfService.java
│ │ │ └── utils/
│ │ │ ├── PdfUtil.java
│ │ │ └── DynamicPdfGeneratorApplication.java
│ ├── resources/
│ │ ├── static/
│ │ ├── templates/
│ │ │ ├── error/
│ │ │ └── invoice.html
│ │ └── application.properties
│
├── test/
│ ├── java/
│ │ └── com/
│ │ └── freightfox/
│ │ └── pdfgenerator/
│ │ └── PdfServiceTest.java
│ └── resources/
│
├── mvnw
├── mvnw.cmd
├── pom.xml
└── README.md
- REST API for PDF Generation: Easily generate PDFs by sending data through a RESTful POST request.
- Dynamic Content Generation: Custom PDFs created using Thymeleaf templates, making it flexible for various document types like invoices, receipts, etc.
- Exception Handling: Graceful error handling with custom exceptions.
- Direct PDF Response: The generated PDF is returned directly in the response body, ready for immediate download.
- Logging: Integrated with SLF4J for effective logging and debugging.
- Spring Boot – Backend framework for developing the REST API.
- Thymeleaf – Template engine for dynamic PDF generation.
- Swagger – API documentation.
- Java 21 – Latest features of Java for enhanced performance and productivity.
- SLF4J – Logging framework.
- Maven – Dependency management and build tool.
/src/main/java/com/freightfox/pdfgenerator/ – Contains the core Java files, including controllers, services, configurations, and utility classes.
- controller/ – Contains the PdfController.java that handles API requests.
- service/* – Contains business logic for generating PDFs (PdfService.java).
- model/ – Data model for incoming PDF generation requests (PdfRequest.java).
- config/ – Configuration files for application setup, Swagger, and web settings.
- exception/ – Global exception handling for REST APIs.
- utils/ – Utility classes such as PdfUtil.java that help with PDF operations.
- /src/main/resources/ – Contains application resources.
- templates/ – Thymeleaf templates for generating dynamic PDFs (e.g., invoice.html).
- application.properties – Configuration file for application properties.
- /test/ – Test folder with unit tests.
- URL:
/api/pdf/generate
- Method:
POST
- Request Body: JSON data containing the fields required for PDF generation
- Response: Binary PDF file returned in the response body
Example Request:
{
"seller": "Seller Name",
"sellerGstin": "GSTIN123",
"sellerAddress": "Seller Address",
"buyer": "Buyer Name",
"buyerGstin": "GSTIN456",
"buyerAddress": "Buyer Address",
"items": [],
"template": "invoice.html"
}
Example Response:
The response will be a binary PDF file, like Buyer_Name_1.pdf
.
- Clone the Repository:
git clone https://github.com/your-username/DynamicPDFGenerator.git
- Navigate into the Project Directory:
cd DynamicPDFGenerator
- Build the Project using Maven:
mvn clean install
- Run the Application:
The API will be available at: http://localhost:8080/api/pdf/generate
mvn spring-boot:run
- Method:
POST
- URL: http://localhost:8080/api/pdf/generate
- Content-Type:
application/json
{
"seller": "Seller Name",
"sellerGstin": "GSTIN123",
"sellerAddress": "Seller Address",
"buyer": "Buyer Name",
"buyerGstin": "GSTIN456",
"buyerAddress": "Buyer Address",
"items": [],
"template": "invoice.html"
}
You will receive a binary PDF file directly in the response.
Sample PDF Output:
Buyer_Name_1.pdf