Skip to content

CobyApp/report

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

14 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

PDF Template Automation Engine

A web application that automatically maps data to PDF templates to generate completed PDFs.

Version Python React License

๐Ÿ“‹ Table of Contents

โœจ Key Features

๐ŸŽฏ Core Features

  • ๐Ÿ“„ PDF Template Upload: Upload and manage A4 PDF templates
  • ๐ŸŽจ Visual Field Mapping: Drag and drop to specify data paths for template fields
  • โšก Real-time Preview: Instantly check field placement
  • ๐Ÿ’พ Real-time Testing: Test rendering before saving
  • ๐Ÿ”„ Automatic PDF Generation: Automatically generate completed PDFs from JSON data
  • ๐Ÿ”Œ REST API: Use via HTTP API from programs

๐Ÿ› ๏ธ Editing Features

  • Property Editing: Real-time adjustment of position (X, Y), size (width, height), font, alignment
  • Field Management: Add, delete, select fields
  • Template Management: Individual/bulk delete support

๐Ÿ—๏ธ Tech Stack

Backend

  • FastAPI (0.104.1) - High-performance Python web framework
  • PyMuPDF (fitz) (1.23.8) - PDF information extraction and image rendering
  • pypdf (3.17.1) - PDF merging
  • Uvicorn - ASGI server

Frontend

  • React (18.2.0) - UI framework
  • Vite (5.0.8) - Fast build tool
  • Axios (1.6.2) - HTTP client

๐Ÿš€ Quick Start

Prerequisites

  • Docker and Docker Compose (for local development)
  • AWS CLI (for deployment)
  • Python 3.9+ and Node.js 16+ (optional, for development without Docker)

1. Clone Repository

git clone https://github.com/CobyApp/report.git
cd report

2. Run with Docker Compose (Recommended)

# Start all services
docker-compose up -d

# View logs
docker-compose logs -f

# Stop all services
docker-compose down

Access:

3. Run Individually (Development)

Terminal 1 - Backend:

cd backend

# Create virtual environment
python3 -m venv venv
source venv/bin/activate  # Windows: venv\Scripts\activate

# Install packages
pip install -r requirements.txt

# Run server
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000

Terminal 2 - Frontend:

cd frontend

# Install packages
npm install

# Run development server
npm run dev

Access:

๐Ÿš€ AWS Deployment

Prerequisites

  1. AWS Account with appropriate permissions
  2. AWS CLI installed and configured
  3. Docker installed and running

Step 1: Setup IAM Roles (First Time Only)

# Setup IAM roles for Elastic Beanstalk
./setup-iam.sh

Step 2: Deploy to AWS

# Deploy to AWS Elastic Beanstalk (Tokyo region)
./deploy.sh

This script will:

  1. Build Docker images for backend and frontend
  2. Push images to AWS ECR (Elastic Container Registry)
  3. Package the application with Dockerfile
  4. Deploy to Elastic Beanstalk environment

Note: First deployment may take 10-15 minutes.

Step 3: Access Deployed Application

After deployment completes, get your application URL:

aws elasticbeanstalk describe-environments \
  --application-name pdf-template-engine \
  --environment-names pdf-template-prod \
  --region ap-northeast-1 \
  --query 'Environments[0].CNAME' \
  --output text

Access your application at: http://<your-cname>.elasticbeanstalk.com

๐Ÿ“– Usage

1. Upload Template

  1. Access the application in your web browser (http://localhost for Docker, http://localhost:3000 for dev)
  2. Register or login to your account
  3. Click "Upload PDF Template" button
  4. Select A4 PDF template file

2. Field Mapping

  1. Click uploaded template card to enter edit mode
  2. Drag on PDF preview to select field area
  3. Enter data path in input popup (e.g., customer.name, items[0].price)
  4. Add fields as needed

3. Property Editing

  1. Click field to select
  2. Modify in right property panel:
    • Data Path: JSON path to map to field
    • X, Y: Field position (PDF coordinates)
    • Width, Height: Field size
    • Font Size: Text size
    • Alignment: Left/Center/Right

4. Test Rendering

  1. Click "๐Ÿงช Test Rendering" button
  2. Enter values for each field (prompt)
  3. Completed PDF automatically downloads
  4. Changes are reflected before saving

5. Save

  1. Click "๐Ÿ’พ Save" button
  2. Template mapping information is saved to server

๐Ÿ”ค Font Configuration

This application supports automatic font selection based on text language:

  • Japanese: MS Gothic (government standard) โ†’ MS Mincho โ†’ Noto Sans JP
  • Korean: Malgun Gothic โ†’ Nanum Gothic โ†’ Noto Sans KR
  • English: Times-Roman (formal documents)

Font Installation

Quick Setup (Recommended):

# Run the automatic font installation script
./install-fonts.sh

This script will download:

  • Nanum Gothic (Korean)
  • Noto Sans JP (Japanese, if not already present)
  • Noto Sans KR (Korean, if not already present)

Manual Installation:

  1. Download required fonts (see FONTS.md for direct download links)
  2. Copy font files to backend/fonts/ directory
  3. Restart the application

For Mac/Linux users:

  • The install-fonts.sh script downloads open-source fonts automatically
  • For MS Gothic/Mincho and Malgun Gothic, copy from Windows: C:\Windows\Fonts\

Note: The application includes Noto Sans JP and Noto Sans KR fonts by default. For Japanese government document compatibility, install MS Gothic or MS Mincho fonts. See FONTS.md for detailed instructions and download links.

๐Ÿ“ก API Documentation

Endpoint List

Method Path Description
POST /api/templates Upload PDF template
GET /api/templates List templates
GET /api/templates/{id} Get template details
PUT /api/templates/{id}/mapping Save template mapping
POST /api/render/{id} Generate PDF (requires data)
GET /api/templates/{id}/preview Page preview image
DELETE /api/templates/{id} Delete template
DELETE /api/templates Delete all templates

Usage Examples

Upload Template

curl -X POST http://localhost:8000/api/templates \
  -F "file=@template.pdf"

Response:

{
  "template_id": "uuid-here",
  "filename": "template.pdf",
  "page_count": 1,
  "page_size": {"w_pt": 595.28, "h_pt": 841.89}
}

Save Template Mapping

curl -X PUT http://localhost:8000/api/templates/{template_id}/mapping \
  -H "Content-Type: application/json" \
  -d '{
    "elements": [
      {
        "id": "elem1",
        "type": "text",
        "page": 1,
        "bbox": {"x": 100, "y": 100, "w": 200, "h": 20},
        "data_path": "customer.name",
        "style": {"font": "Helvetica", "size": 10, "align": "left"}
      }
    ]
  }'

Generate PDF

Generate a completed PDF by providing field data that matches your template's data paths.

Request:

curl -X POST http://localhost:8000/api/render/{template_id} \
  -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "customer": {
      "name": "John Doe",
      "email": "john@example.com",
      "address": "123 Main St"
    },
    "items": [
      {"name": "Item 1", "price": 10000, "quantity": 2},
      {"name": "Item 2", "price": 20000, "quantity": 1}
    ],
    "checked": true,
    "total": 40000,
    "date": "2024-01-17"
  }' \
  --output result.pdf

Response:

  • Content-Type: application/pdf
  • Body: Binary PDF file
  • Filename: rendered_{template_id}.pdf

Request Body Structure:

{
  // Field data that matches template data paths
  // Example: if template has "customer.name", provide:
  "customer": {
    "name": "John Doe"
  },
  
  // Array fields: use "items[0].price" in template
  "items": [
    {"name": "Item 1", "price": 10000}
  ],
  
  // Simple boolean fields
  "checked": true,
  
  // Optional: Override template elements for testing
  "_elements": [
    {
      "id": "elem1",
      "type": "text",
      "page": 1,
      "bbox": {"x": 100, "y": 100, "w": 200, "h": 20},
      "data_path": "customer.name"
    }
  ]
}

Error Responses:

  • 400 Bad Request: Invalid data or template structure

    {"detail": "Template structure error: ..."}
  • 401 Unauthorized: Authentication required

    {"detail": "Authentication required"}
  • 403 Forbidden: Template does not belong to user

    {"detail": "Access denied"}
  • 404 Not Found: Template not found

    {"detail": "Template not found"}

Real-time elements transmission (test rendering):

Include _elements in the request body to override template elements without saving:

curl -X POST http://localhost:8000/api/render/{template_id} \
  -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "customer": {"name": "John Doe"},
    "_elements": [
      {
        "id": "elem1",
        "type": "text",
        "page": 1,
        "bbox": {"x": 100, "y": 100, "w": 200, "h": 20},
        "data_path": "customer.name"
      }
    ]
  }' \
  --output result.pdf

API Documentation:

For interactive API documentation and detailed request/response schemas, visit:

๐Ÿ“ Project Structure

report/
โ”œโ”€โ”€ backend/                 # FastAPI backend
โ”‚   โ”œโ”€โ”€ app/
โ”‚   โ”‚   โ”œโ”€โ”€ main.py         # FastAPI app and API endpoints
โ”‚   โ”‚   โ””โ”€โ”€ services/
โ”‚   โ”‚       โ”œโ”€โ”€ pdf_service.py      # PDF processing (upload, preview)
โ”‚   โ”‚       โ”œโ”€โ”€ template_service.py # Template save/load
โ”‚   โ”‚       โ”œโ”€โ”€ render_service.py   # PDF rendering engine
โ”‚   โ”‚       โ””โ”€โ”€ auth_service.py    # Authentication service
โ”‚   โ”œโ”€โ”€ templates/          # Template JSON storage (auto-generated)
โ”‚   โ”œโ”€โ”€ uploads/            # Uploaded PDFs and generated PDFs (auto-generated)
โ”‚   โ”œโ”€โ”€ users/              # User data (auto-generated)
โ”‚   โ”œโ”€โ”€ fonts/              # Font files (NotoSansJP, NotoSansKR)
โ”‚   โ””โ”€โ”€ requirements.txt    # Python package dependencies
โ”‚
โ”œโ”€โ”€ frontend/               # React frontend
โ”‚   โ”œโ”€โ”€ src/
โ”‚   โ”‚   โ”œโ”€โ”€ App.jsx         # Main app component
โ”‚   โ”‚   โ”œโ”€โ”€ components/     # React components
โ”‚   โ”‚   โ”œโ”€โ”€ contexts/       # React contexts (Auth)
โ”‚   โ”‚   โ””โ”€โ”€ i18n/           # Internationalization
โ”‚   โ”œโ”€โ”€ nginx.conf          # Nginx configuration
โ”‚   โ”œโ”€โ”€ package.json        # Node.js package dependencies
โ”‚   โ””โ”€โ”€ vite.config.js      # Vite configuration
โ”‚
โ”œโ”€โ”€ .ebextensions/          # Elastic Beanstalk configuration
โ”‚   โ”œโ”€โ”€ 01_nginx.config     # Nginx configuration
โ”‚   โ””โ”€โ”€ 02_storage.config   # Storage configuration
โ”‚
โ”œโ”€โ”€ Dockerfile              # Combined Dockerfile for production
โ”œโ”€โ”€ docker-compose.yml      # Docker Compose configuration
โ”œโ”€โ”€ deploy.sh               # AWS deployment script
โ”œโ”€โ”€ setup-iam.sh            # IAM roles setup script
โ””โ”€โ”€ README.md               # This file

๐Ÿ“ Template JSON Structure

Templates are saved in JSON format:

{
  "template_id": "uuid",
  "filename": "template.pdf",
  "page_size": {
    "w_pt": 595.28,
    "h_pt": 841.89
  },
  "pages": [
    {
      "page": 1,
      "width": 595.28,
      "height": 841.89,
      "width_pt": 595.28,
      "height_pt": 841.89
    }
  ],
  "elements": [
    {
      "id": "elem_1234567890",
      "type": "text",
      "page": 1,
      "bbox": {
        "x": 100,
        "y": 200,
        "w": 200,
        "h": 20
      },
      "data_path": "customer.name",
      "style": {
        "font": "Helvetica",
        "size": 10,
        "align": "left"
      },
      "overflow": {
        "mode": "shrink_to_fit",
        "min_size": 7
      }
    }
  ],
  "created_at": "2026-01-17T..."
}

Field Descriptions

  • bbox: Field position and size (PDF coordinate system, point units)
    • x, y: Top-left corner coordinates (stored in screen coordinates, converted during rendering)
    • w, h: Width, height
  • data_path: JSON data path (e.g., customer.name, items[0].price)
  • style: Text style settings
  • overflow: Text overflow handling (currently supports shrink_to_fit)

โœ… Supported Features

  • โœ… Text Fields: Data path mapping, alignment, auto-shrink
  • โœ… Checkboxes: Boolean value display
  • โœ… Repeat Tables: List data repeat rendering
  • โœ… Multi-page: Multiple page support
  • โœ… Real-time Editing: Test before saving
  • โœ… Property Editing: Real-time adjustment of position, size, style
  • โœ… User Authentication: Login/registration for user-specific data management

๐Ÿ”ฎ Future Improvements

  • Image fields (signatures, stamps, QR codes)
  • Conditional display (if statements)
  • Automatic page overflow handling
  • Improved CJK font support
  • Rich text (partial bold, colors, etc.)
  • Data schema validation UI
  • Template version management

๐Ÿ› Troubleshooting

Docker Issues

# Check Docker is running
docker ps

# View container logs
docker-compose logs -f

# Restart containers
docker-compose restart

# Rebuild containers
docker-compose up -d --build

Port Already in Use

# Check ports
lsof -ti:8000  # Backend
lsof -ti:80    # Frontend (Docker)
lsof -ti:3000  # Frontend (Dev mode)

# Kill processes
kill -9 $(lsof -ti:8000)
kill -9 $(lsof -ti:80)
kill -9 $(lsof -ti:3000)

AWS Deployment Issues

# Check deployment status
aws elasticbeanstalk describe-environments \
  --application-name pdf-template-engine \
  --environment-names pdf-template-prod \
  --region ap-northeast-1

# View deployment logs
aws elasticbeanstalk request-environment-info \
  --application-name pdf-template-engine \
  --environment-name pdf-template-prod \
  --info-type tail \
  --region ap-northeast-1

Package Installation Errors

Backend:

# Check virtual environment
python3 -m venv venv
source venv/bin/activate
pip install --upgrade pip
pip install -r requirements.txt

Frontend:

# Clear cache and reinstall
rm -rf node_modules package-lock.json
npm install

๐Ÿค Contributing

Issues and pull requests are welcome!

๐Ÿ“„ License

MIT License


Project Link: https://github.com/CobyApp/report

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published