This project provides a FastAPI-based microservice that accepts stamp SVG files, arranges them in a grid on A4 PDF pages, and returns the generated PDF. Each SVG is sized precisely and optionally grouped by stamp dimensions.
- Upload SVGs with stamp dimensions
- Arrange stamps with margin and padding in a grid
- Group stamps by size to avoid layout distortion
- Convert stamps to precise A4-sized PDFs
- Return generated PDFs via API
- Python 3.12
- Clone the Repository
git clone https://github.com/kha333n/svg_to_pdf_api.git
cd svg_to_pdf_api- Create a Virtual Environment
python3 -m venv venv
# Activate the virtual environment on Linux/Mac
source venv/bin/activate
# Activate the virtual environment on Windows
venv\Scripts\activate- Install Dependencies
pip install -r requirements.txt- Run the Server
uvicorn main:app --reload --port 8081Upload a single SVG file with size and session info.
session_id(string, required): Session key to group files.width_mm(float, required): Width of the stamp in mm.height_mm(float, required): Height of the stamp in mm.file(file, required): SVG file upload.
{
"message": "SVG uploaded",
"stamp_id": "abc123..."
}Generate a PDF from previously uploaded SVGs grouped by stamp size.
session_id(string, required)
- A4-sized PDF file as direct download.
Generate individual PDFs page for each SVG in the session.
session_id(string, required)
- PDF file as direct download.
Remove all uploaded SVGs from the session (including outputs).
session_id(string, required)
{
"message": "Session reset"
}svg_to_pdf_sessions/
└── <session_id>/
├── abc123.svg
├── abc123.meta
└── output/
└── stamps_40x15.pdf
File main.py
You can customize margins and spacing directly in the code:
PAGE_MARGIN_MM: Outer margin on A4 pageCELL_MARGIN_MM: Space between each stamp
You can use the included script to test by pointing it to a directory of SVGs:
python test.py