A FastAPI service for processing PDF files with dielines/stanslines. The service can detect existing dielines, analyze PDF properties, and modify or replace dielines based on shape specifications.
- PDF Analysis: Extract dimensions, trimbox, and detect existing dielines
- Shape Support:
- Circle/Oval
- Rectangle (with optional corner radius)
- Custom/Irregular shapes
- Spot Color Handling: Detect and manipulate spot colors (CutContour, KissCut, stans, etc.)
- Dieline Processing:
- For circles/rectangles: Remove existing dieline and add new one
- For custom shapes: Keep existing shape but rename spot color
- Standards Compliant: Creates dielines with 0.5pt 100% magenta lines with overprint
- Python 3.10+
- UV package manager
- Clone the repository:
git clone <repository-url>
cd OGOS_fastapi_pdfmodule
- Install dependencies using UV:
uv sync
- Run the development server:
uv run uvicorn main:app --reload
The API will be available at http://localhost:8000
POST /api/pdf/analyze
Analyzes a PDF file and returns information about its dimensions, trimbox, and detected dielines.
Request:
- Form data with PDF file upload
Response:
{
"pdf_size": {"width": 595.0, "height": 842.0},
"page_count": 1,
"trimbox": {"x0": 0, "y0": 0, "x1": 595, "y1": 842},
"mediabox": {"x0": 0, "y0": 0, "x1": 595, "y1": 842},
"detected_dielines": [...],
"spot_colors": ["CutContour"],
"has_cutcontour": true
}
POST /api/pdf/process
Processes a PDF file with dieline modifications based on job configuration.
Request:
pdf_file
: PDF file uploadjob_config
: JSON string with configuration
Example job_config:
{
"reference": "5355531-8352950",
"shape": "circle",
"width": 50,
"height": 50,
"radius": 0,
"spot_color_name": "stans",
"line_thickness": 0.5
}
Response:
- Processed PDF file download
POST /api/pdf/process-with-json-file
Processes a PDF file using a separate JSON configuration file (compatible with example JSON format).
Request:
pdf_file
: PDF file uploadjson_file
: JSON configuration file upload
Response:
- Processed PDF file download
The application can be configured using environment variables or a .env
file:
API_TITLE=PDF Dieline Processor
API_VERSION=1.0.0
MAX_FILE_SIZE=104857600 # 100MB in bytes
DEFAULT_SPOT_COLOR=stans
DEFAULT_LINE_THICKNESS=0.5
LOG_LEVEL=INFO
The service accepts JSON configuration in the following format:
{
"ReferenceAtCustomer": "5355531-8352950",
"Description": "labels_on_roll",
"Shape": "circle",
"Width": 50,
"Height": 50,
"Radius": 0,
"Substrate": "mat wit PP",
"Adhesive": "permanent",
"Colors": "CMYK",
"Winding": 2
}
"circle"
: Creates circular or oval dieline"rectangle"
: Creates rectangular dieline with optional corner radius"custom"
: Preserves existing dieline shape, only renames spot color
OGOS_fastapi_pdfmodule/
├── app/
│ ├── api/
│ │ └── endpoints/
│ │ └── pdf.py # API endpoints
│ ├── core/
│ │ ├── config.py # Configuration
│ │ ├── pdf_analyzer.py # PDF analysis
│ │ ├── pdf_processor.py # Main processing logic
│ │ └── shape_generators.py # Shape generation
│ ├── models/
│ │ └── schemas.py # Pydantic models
│ └── utils/
│ ├── pdf_utils.py # PDF utilities
│ └── spot_color_handler.py # Spot color manipulation
├── examplecode/ # Example PDFs and scripts
├── main.py # FastAPI application
├── pyproject.toml # Project dependencies
└── README.md # This file
Test the API using the provided HTTP file:
# Run the server
uv run uvicorn main:app --reload
# Test endpoints using test_main.http
Or use curl:
# Analyze a PDF
curl -X POST "http://localhost:8000/api/pdf/analyze" \
-H "accept: application/json" \
-F "[email protected]"
# Process a PDF
curl -X POST "http://localhost:8000/api/pdf/process" \
-H "accept: application/pdf" \
-F "[email protected]" \
-F 'job_config={"reference":"test-001","shape":"circle","width":50,"height":50}'
The service detects dielines by looking for:
- Spot colors named: CutContour, KissCut, stans, DieCut (and variations)
- Thin stroke-only paths (≤1.0pt line width)
- Paths without fill color
- Uses PyMuPDF for PDF analysis and path extraction
- Uses pypdf for spot color detection
- Uses ReportLab for generating new dieline shapes
- Preserves original PDF content while modifying only dieline elements
All dielines are created with:
- 100% Magenta (CMYK: 0, 1, 0, 0)
- 0.5pt line thickness (configurable)
- Overprint enabled
- Custom spot color name (default: "stans")
- Custom Shape Processing: Currently copies the PDF without full spot color renaming (placeholder implementation)
- Multi-page PDFs: Optimized for single-page label PDFs
- Complex Paths: May not detect all types of complex dieline paths
- Full implementation of spot color renaming for custom shapes
- Support for multi-page PDF processing
- Batch processing API endpoint
- Advanced dieline detection algorithms
- WebSocket support for real-time processing status
[Your License Here]
For issues or questions, please contact [Your Contact Info]