This project is a Spring Boot application that generates a dynamic PDF invoice using data provided via a REST API. The invoice contains seller, buyer, and item details, all organized into tables and neatly formatted using the iText library.
- Accepts seller, buyer, and item data via a REST API.
- Dynamically generates a well-formatted PDF invoice.
- Stores the generated PDF in a publicly accessible directory (
C:/Users/Public/Documents/
). - Allows the user to download the generated PDF via a REST API.
- Easily configurable for different platforms and directories.
- Spring Boot: For creating RESTful APIs.
- iText: For generating PDF documents.
- Maven: For dependency management and building the project.
- Java (Version 8 or later).
src ├── main │ ├── java │ │ └── com.example.pdfgenerator │ │ ├── controller │ │ │ └── PdfController.java # Handles API requests │ │ ├── model │ │ │ └── PdfRequest.java # Represents request data (Seller, Buyer, Items) │ │ ├── service │ │ │ └── PdfService.java # Service class to generate PDF │ └── resources │ └── application.properties # Configuration └── test └── java └── com.example.pdfgenerator └── PdfControllerTest.java # Unit tests for the controller
git clone https://github.com/your-username/pdf-generator.git cd pdf-generator
Ensure you have Maven installed. Run the following command to build the project:
mvn clean install
Start the Spring Boot application using:
mvn spring-boot:run
Use Postman or Curl to interact with the API.
Send a POST request to generate a PDF. The data you provide in the request body will be used to generate the invoice.
- Endpoint:
/api/pdf/generate
- Method:
POST
- Request Body:
{ "seller": "ABC Pvt. Ltd.", "sellerGstin": "22ABCDE1234Z5A1", "sellerAddress": "New Delhi, India", "buyer": "XYZ Computers", "buyerGstin": "29ABCDE9876Z1A2", "buyerAddress": "Bangalore, India", "items": [ { "name": "Laptop", "quantity": "1 Nos", "rate": 55000.00, "amount": 55000.00 }, { "name": "Mouse", "quantity": "5 Nos", "rate": 200.00, "amount": 1000.00 } ] }
Response:
{ "message": "PDF generated successfully.", "downloadUrl": "/api/pdf/download?file=C:/Users/Public/Documents/invoice_1697241905001.pdf" }
Use the download URL provided in the response above to download the generated PDF:
- Endpoint:
/api/pdf/download?file=C:/Users/Public/Documents/invoice_1697241905001.pdf
- Method:
GET
The PDF will look similar to the following:
File Storage Path: The generated PDF will be stored in the directory C:/Users/Public/Documents/
. You can configure this path if needed.
If you'd like to change the path, modify the directory in PdfService.java
:
String directoryPath = "C:/Users/Public/Documents"; // Default public location
To ensure compatibility on other operating systems (Linux, macOS), this path can be modified by detecting the OS dynamically:
String directoryPath = System.getProperty("user.home") + File.separator + "Documents";