Skip to content

Latest commit

 

History

History
136 lines (114 loc) · 4.59 KB

README.md

File metadata and controls

136 lines (114 loc) · 4.59 KB

Dynamic PDF Invoice Generator using Spring Boot and iText

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.

Features

  • 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.

Technologies

  • Spring Boot: For creating RESTful APIs.
  • iText: For generating PDF documents.
  • Maven: For dependency management and building the project.
  • Java (Version 8 or later).

Project Structure

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

How to Use

1. Clone the repository

git clone https://github.com/your-username/pdf-generator.git
cd pdf-generator

2. Build the project

Ensure you have Maven installed. Run the following command to build the project:

mvn clean install

3. Run the Application

Start the Spring Boot application using:

mvn spring-boot:run

4. Testing the API

Use Postman or Curl to interact with the API.

Generate PDF

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"
}

Download the 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

Example Generated PDF

The PDF will look similar to the following:

GrowthX Logo GrowthX Logo

Configuration

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

Cross-Platform Support

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";