Skip to content

Spring Boot REST API application to dynamically generate and download PDF from the given input data

Notifications You must be signed in to change notification settings

sushnag22/pdf-generator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PDF Generator REST API

A Java Spring Boot 3 application that generates, stores, and downloads PDF files using a Thymeleaf template. This API is designed to generate dynamic PDFs from JSON input and store them on the server, providing efficient file management by reusing already generated PDFs.

Features

  • PDF Generation: Generate PDF documents from JSON input using Thymeleaf templating.
  • PDF Storage: Store the generated PDFs in a designated directory on the server.
  • Efficient PDF Retrieval: Hash the input data to generate unique file names, preventing regeneration of identical PDFs.
  • PDF Download: Download generated PDFs directly from the server.
  • Validation: Input data is validated to ensure consistency and correctness before generating PDFs.

API Documentation

The API is hosted on Render and can be accessed at PDF Generator API Documentation.

API Endpoints:

  1. Generate and Store PDF

    • Endpoint: /api/v1/pdf/generate-and-store
    • Method: POST
    • Description: Generates a PDF based on the input data and stores it on the server.
    • Request Body: JSON input data
    {
        "sellerName": "Example Seller",
        "sellerAddress": "123 Seller Street",
        "sellerGstin": "123456789012345",
        "buyerName": "Example Buyer",
        "buyerAddress": "456 Buyer Avenue",
        "buyerGstin": "543210987654321",
        "items": [
            {
                "name": "Product A",
                "quantity": 10,
                "rate": 25.5,
                "amount": 255
            }
        ]
    }
    • Response:
      • Success (200 OK):
        {
          "status": "Success",
          "statusCode": 200,
          "message": "PDF generated and stored successfully",
          "fileName": "Example_Seller_Example_Buyer_hash.pdf"
        }
      • Failure
        • 400 Bad Request:
        {
          "status": "Error",
          "statusCode": 400,
          "message": "Invalid input data. Please correct the data: {error message}"
        }
        • 500 Internal Server Error:
         {
           "status": "Error",
           "statusCode": 500,
           "message": "Something went wrong. Please try again later."
         }
  2. Download PDF

    • Endpoint: /api/v1/pdf/download/{fileName}
    • Method: GET
    • Description: Downloads the PDF file with the specified file name.
    • Response:
      • Success (200 OK): PDF file is returned as binary.
      • Failure
        • 404 Not Found: PDF file not found.
        • 500 Internal Server Error: Internal server error.

Tech Stack

  • Java
  • Spring Boot
  • Thymeleaf
  • SLF4J
  • Flying Saucer
  • SpringDoc OpenAPI
  • Mockito
  • JUnit

Project Structure

src
├── main
│   ├── java
│   │   └── io.github.sushnag22.pdfgenerator
│   │       ├── controller          # REST controllers
|   |       |── exception           # Custom exceptions
│   │       ├── model               # Data models
│   │       ├── service             # Business logic
│   │       ├── util                # Utility classes
|   |       └── validation          # Validation classes
│   ├── resources
│   │   └── pdf_template.html       # Thymeleaf HTML template for PDF generation
│   └── resources
│       └── application.properties  # Application configuration
├── test
│   ├── java
│   │   └── io.github.sushnag22.pdfgenerator
│   │       ├── controller          # Controller tests
│   │       ├── service             # Service tests
│   │       ├── util                # Utility tests
│   │       └── validation          # Validation tests

How It Works

  1. PDF Generation: When a POST request is sent to /generate-and-store, the API receives the input data, validates it, and uses Thymeleaf to generate a PDF. If the same data is provided again, the API returns the previously generated PDF without regenerating it.
  2. File Storage: The PDF files are stored in a predefined directory (pdf.storage.path in the application configuration file). The filenames are generated based on a combination of the seller's and buyer's names and a hash of the input data.
  3. PDF Download: A GET request to /download/{fileName} returns the requested PDF file if it exists.

Installation

  1. Pre-requisites:

    • JDK 21 (or higher)
    • Git
  2. Clone the repository:

    git clone https://github.com/sushnag22/pdf-generator.git
  3. Change the directory:

    cd pdf-generator
  4. Set the application configuration in src/main/resources/application.properties:

    # PDF storage directory
    pdf.storage.path=/path/to/pdf/storage/directory
    
    # Unit of measurement for the quantity of items
    item.quantity.unit=Nos
    
    # Currency symbol for the rate and amount of items
    currency.format=INR
    
    # SpringDoc OpenAPI configuration
    springdoc.api-docs.path=/api/v1/api-docs
    springdoc.swagger-ui.path=/api/v1/swagger-ui.html
  5. Build the project:

    ./gradlew build
  6. Run the application:

    ./gradlew bootRun

The application will start on http://localhost:8080.

Releases

No releases published

Packages

No packages published