KDP Builder is a local-first toolkit for generating professional, print-ready interiors (and soon covers) for KDP. It focuses on speed, repeatability, and 100% vector output using ReportLab. Start with a CLI to create lined journals, then expand into grids, trackers, and AI-generated layouts.
- Vector interiors: Crisp, scalable PDFs suitable for print.
- KDP-aware sizing: Trim sizes and safe margins built-in (e.g.,
6x9). - Fast CLI: Generate 20–150+ page interiors in seconds.
- Templates:
lined,grid,dot,habittemplates available via CLI. - Parity-aware safe area: Even/odd pages mirror inner/outer margins; optional gutter.
- Extensible: Add validators, AI layout gen, and covers.
- Local-first: Works offline; no API keys required for the MVP.
- AI vision (optional): Enhanced element detection via DocLayNet + Ollama VLM for semantic labeling.
To use a local DocLayNet YOLOv8 model for better table/text/title detection:
- Place your weights file at
models/doclayout/yolov8_doclaynet.pt. - Restart the API server; it will auto-load via
DOCLAYOUT_WEIGHTS.
If you use a different path, set the environment before starting:
export DOCLAYOUT_WEIGHTS=/absolute/path/to/your_weights.pt
uvicorn web.backend.main:app --reload --port 8000main.py— CLI entrypoint (Click-based).kdp_builder/config/sizes.py— Trim sizes, margins, bleed flags.kdp_builder/renderer/pdf_renderer.py— Vector rendering with multiple templates.kdp_builder/renderer/templates.py— Template primitives (lined, grid, dot, habit).kdp_builder/__init__.py— Package marker.
Planned modules:
kdp_builder/validator/— KDP checks (safe area, line weights, DPI).kdp_builder/layout/— AI layout generator (Ollama, Bedrock).kdp_builder/cover/— Vector/SDXL covers.
- Create and activate a virtual environment (macOS):
cd KDP_builder
python3 -m venv .venv
source .venv/bin/activate- Install minimal dependencies:
pip install -r requirements.txt- Generate your first interior (6x9, 120 pages) — defaults to
outputs/interior.pdf:
python main.py --trim 6x9 --pages 120 --out outputs/interior.pdf- Customize spacing and line weight:
python main.py --line-spacing-pt 20 --line-weight-pt 0.6 --out interior_dense.pdfUse --template and related flags:
- Lined (default):
python main.py --template lined --pages 4 --gutter-pt 18- Grid:
python main.py --template grid --grid-size-pt 18 --pages 2 --out outputs/grid.pdf- Dot grid:
python main.py --template dot --dot-step-pt 18 --dot-radius-pt 0.6 --pages 2 --out outputs/dot.pdf- Habit tracker:
python main.py --template habit --habit-rows 20 --habit-cols 7 --pages 2 --out outputs/habit.pdfGenerate custom layouts using local AI (Ollama) with intelligent fallback systems. Perfect for creating unique planners, trackers, and notebooks.
- Local AI: Uses Ollama (llama3.2 or tinyllama) for fast, private generation
- Intelligent Fallbacks: If AI fails, automatically generates basic layouts (habit tracker, weekly planner, lined pages)
- KDP Compliant: Automatic 36pt gutter for proper binding, bleed enabled, 4 pages by default
- Smart Parsing: Robust JSON cleaning handles malformed AI responses
- Install and start Ollama:
# Install Ollama (one-time)
curl -fsSL https://ollama.com/install.sh | sh
# Start Ollama service
brew services start ollama
# Pull a model (tinyllama is faster, llama3.2 is more capable)
ollama pull tinyllama- Generate AI layouts:
python main.py --ai-prompt "Create a habit tracker with 5 habits and 7 days" --out outputs/ai_habit.pdf- 4 pages generated automatically
- 36pt gutter for KDP binding compliance
- Bleed enabled (9pt standard)
- Fallback layouts if AI fails
# Habit tracker
python main.py --ai-prompt "Create a habit tracker with 5 habits and 7 days" --out outputs/habit_ai.pdf
# Weekly planner
python main.py --ai-prompt "Create a weekly planner with hourly slots from 8 AM to 8 PM" --out outputs/planner_ai.pdf
# Goal tracker
python main.py --ai-prompt "Create a goal tracker with 10 goals and monthly checkboxes" --out outputs/goals_ai.pdf
# Custom notebook
python main.py --ai-prompt "Create a lined notebook with headers and footers" --out outputs/notebook_ai.pdfOverride defaults if needed:
python main.py --ai-prompt "Create a complex habit tracker" --pages 8 --gutter-pt 18 --out custom_ai.pdf--trimTrim key (default:6x9). Defined inkdp_builder/config/sizes.py.--pagesNumber of pages (default:120, min1).--outOutput PDF path (default:outputs/interior.pdf).--line-spacing-ptLine spacing in points (default:18.0).--line-weight-ptStroke width in points (default:0.5).--gutter-ptExtra inner margin added to binding side (odd/even mirrored).--templateOne oflined,grid,dot,habit.--grid-size-ptGrid cell size (grid).--dot-step-pt,--dot-radius-ptDot spacing and radius (dot).--habit-rows,--habit-colsHabit matrix size (habit).--page-numbersAdd page numbers on the outer side (mirrored even/odd).--header,--footerCentered header/footer text within safe area.--header-font-size,--footer-font-size,--page-number-font-sizeTypography controls for pagination.--set-trimboxWrite TrimBox equal to the safe area (for QA in viewers that show boxes).--set-bleedboxWrite BleedBox around TrimBox by--bleed-pt(clamped to MediaBox).--bleed-ptBleed amount in points (72pt = 1 inch).- Cover generation:
--make-coverGenerate a cover instead of interior--cover-pagesInterior page count for spine width--cover-paperPaper type:white|cream|color--cover-bleed-ptCover bleed (points); e.g. 9pt = 0.125"--cover-title,--cover-subtitle,--cover-author
--ai-promptPrompt for AI-generated layout (e.g., 'Create a habit tracker with 30 days'). Uses Ollama for local generation.--validate-pathValidate an existing PDF and exit.--validate-trimTrim key used for validation (defaults to--trim).--validate-verbosePrint verbose diagnostics during validation (Do/Form counts, DPI placements).
See help:
python main.py --help- Layouts are defined procedurally and rendered with ReportLab into vector PDFs compliant with typical KDP trims and margins.
- The MVP now includes multiple templates (lined, grid, dot grid, habit tracker) with mirrored safe areas and optional gutter.
Use --validate-path (interior) or --validate-cover-path (cover). Current checks include:
- Page count within KDP interior range (24–828)
- Uniform page size vs
--trim - PDF encryption status
- PDF header version (warn > 1.7)
- Rotation/orientation warnings
- Annotations/form fields not allowed
- TrimBox/BleedBox sanity (inside MediaBox; Trim inside Bleed)
- Embedded images count and small intrinsic size heuristic (<900 px)
- Fonts: non-embedded (error), Type3 (warning), subset fonts (info)
- Image DPI estimation: uses pikepdf for robust parsing, handles indirect objects, extracts images directly, reports per-placement DPI with thresholds (≥300 recommended, ≥200 required)
Add page numbers, header, and footer:
python main.py --trim 6x9 --pages 24 --template lined --gutter-pt 18 \
--page-numbers --header "My Journal" --footer "www.example.com" \
--header-font-size 12 --footer-font-size 10 --page-number-font-size 10 \
--out outputs/paginated_24.pdfPage numbers render on the outer side (right on odd pages, left on even).
Optionally write TrimBox/BleedBox to the PDF for better QA in compatible viewers:
python main.py --trim 6x9 --pages 2 --template grid --grid-size-pt 18 \
--gutter-pt 18 --set-trimbox --set-bleedbox --bleed-pt 9 \
--out outputs/boxes.pdfThis sets TrimBox to the mirrored safe area and BleedBox to TrimBox expanded by bleed-pt (e.g., 9pt = 0.125").
Generate a full-wrap cover (back + spine + front) with a simple placeholder layout:
python main.py --make-cover --trim 6x9 --cover-pages 120 --cover-paper white \
--cover-bleed-pt 9 --cover-title "My Planner" --cover-subtitle "Undated" \
--cover-author "Author Name" --out outputs/cover.pdfValidate the cover against expected dimensions for the given trim/pages/paper/bleed:
python main.py --validate-cover-path outputs/cover.pdf \
--trim 6x9 --cover-pages 120 --cover-paper white --cover-bleed-pt 9Generated files are written to outputs/ (ignored by git). Override with --out if needed.
- Templates: grid, dot grid, habit/expense trackers. ✅ Implemented
- Validator: safe area checks, line thickness, font embedding. ✅ Implemented
- Covers: vector typography + color systems; optional SDXL artwork.
- AI: Ollama-driven JSON layout generation (local), Bedrock option. ✅ Implemented (Ollama)
- Web UI: FastAPI + React for preview, drag-and-drop adjustments.
- Python: ReportLab, Click (CLI), pikepdf (PDF parsing/validation), requests (Ollama API).
- AI: Ollama (local LLM for layout generation) ✅ Implemented
- Optional/Planned: Typer, FastAPI, Bedrock (AWS AI), Diffusers/SDXL, OpenCV, Tesseract.
- Fork, create a feature branch, and submit a PR.
- Keep PRs focused; include before/after screenshots or sample PDFs when relevant.
- For new templates, add sample outputs to a
samples/folder (git LFS recommended for large assets).
Code: MIT
Models & dataset: “All Rights Reserved”
Always verify KDP guidelines for your chosen trim size, margins, and bleed settings before publishing. Test-print when possible.