PRD-63: Document Generation Module

Status: Draft Priority: P0 — Critical Created: 2026-02-18 Dependencies: PRD-60 (RAG v3), PRD-08 (Document System) Estimated Effort: MVP 16h | Core 28h | Full 42h


Executive Summary

Automatos agents can ingest documents (PDF, DOCX, Markdown) and query them via RAG, but cannot produce polished business documents. This PRD adds a Document Generation module that enables agents and workflows to output professional PDF, DOCX, and XLSX files from data + templates — the "last mile" that turns AI analysis into deliverables businesses actually send to clients.


Part 1: Competitive Landscape — Top 10 Document Generation Tools

Comparison Matrix

#
Tool
Stars
Language
License
Output Formats
Template Approach
FastAPI Fit

1

Typst

46K

Rust

Apache-2.0

PDF

Programmable markup

Moderate (CLI)

2

Pandoc

41.5K

Haskell

GPL-2.0

40+ formats

Markdown conversion

Moderate (CLI)

3

Gotenberg

11.3K

Go

MIT

PDF (from anything)

HTTP API + Chromium/LibreOffice

Excellent

4

WeasyPrint

8.5K

Python

BSD

PDF

HTML + CSS

Excellent

5

python-docx

5.4K

Python

MIT

DOCX

Programmatic API

Excellent

6

pdfme

4K

TypeScript

MIT

PDF

JSON schema + WYSIWYG designer

Frontend only

7

Docxtemplater

3.5K

JavaScript

MIT (core)

DOCX, PPTX

{tag} placeholders

Poor (Node)

8

XlsxWriter

3.5K

Python

BSD

XLSX

Programmatic API

Excellent

9

python-docx-template

2.3K

Python

LGPL-2.1

DOCX

Jinja2 in Word files

Excellent

10

Carbone

1.6K

JavaScript

CCL (restrictive)

15+ formats

Tags in Office files

Blocked (license)

How the Best Tools Work

Typst (46K stars) — The Modern LaTeX Killer

  • Programmable markup language compiled to PDF in milliseconds

  • Loops, conditionals, functions — code meets document

  • Incremental compilation, WASM build available

  • Takeaway: Watch for future; ecosystem too young for enterprise templates today

Gotenberg (11.3K stars) — The Conversion Swiss Army Knife

  • Docker container with Chromium + LibreOffice inside

  • Send HTML/DOCX/XLSX/Markdown via HTTP → get PDF back

  • Stateless, scales horizontally, 50M+ Docker pulls

  • Python client: gotenberg-client on PyPI

  • Takeaway: Perfect conversion sidecar — don't build what Gotenberg already does

WeasyPrint (8.5K stars) — HTML/CSS → PDF, Pure Python

  • pip-installable, BSD license, actively maintained (Feb 2026 release)

  • Full CSS print support: @page rules, headers/footers, page breaks, margins

  • Pairs with Jinja2 for template rendering

  • Takeaway: The go-to Python PDF engine. Jinja2 + WeasyPrint is the standard pattern.

python-docx-template (2.3K stars) — Jinja2 in Word Files

  • Business users design templates in Microsoft Word

  • Developers add {{ variable }} and {% for item in items %} tags

  • Renders to DOCX with full formatting preserved

  • Built on python-docx (already in our requirements.txt)

  • Takeaway: Best DOCX solution. Non-technical users can design templates.

pdfme (4K stars) — WYSIWYG PDF Template Designer

  • React component for visual PDF template design

  • Drag-and-drop fields, JSON schema output

  • MIT license, browser-based

  • Takeaway: Could power a frontend template designer. Generation stays in Python.

Tools We Skip (and Why)

Tool
Reason

Carbone

CCL license prohibits hosted SaaS without commercial license

Docxtemplater

Node.js only; paid modules for images/charts/XLSX — adds language complexity

ReportLab

Too low-level; Jinja2+WeasyPrint achieves same output with 1/5 the code

Pandoc

Document converter, not report generator; GPL friction; requires LaTeX for PDF

Typst

Promising but ecosystem too young; revisit in 6-12 months

2026 Industry Pattern: LLM + Templates

The emerging standard for AI document generation:

No single open-source project packages this end-to-end. The opportunity is to build the pipeline from composable, best-in-class tools.


Part 2: Current Automatos State

What Exists Today

Backend

Component
File
Status

ReportGenerator

orchestrator/modules/tools/services/report_generator.py

✅ Working — Markdown reports with YAML frontmatter

python-docx

requirements.txt

✅ Installed (v1.1.0) — but unused for generation

PandasAI charts

orchestrator/modules/tools/services/pandas_ai_service.py

✅ Working — matplotlib/seaborn PNG charts

Document upload/storage

orchestrator/api/documents.py

✅ Full CRUD, S3 optional

PDF text extraction

pdfplumber, PyPDF2

✅ Working — ingestion only, not generation

Artifact model

orchestrator/core/models/core.py

✅ DB model with kinds: code, text, image, sheet

Frontend

Component
File
Status

Artifact viewer

frontend/components/chatbot/artifact-viewer.tsx

✅ Fullscreen viewer with download (text blob only)

Document management

frontend/components/documents/document-management.tsx

✅ Upload, list, search, tabs

Chart rendering

chart.js, plotly.js, d3, recharts

✅ All installed

Markdown rendering

react-markdown, remark-gfm

✅ Working

What's Missing

Capability
Gap

PDF generation

No WeasyPrint, no HTML→PDF pipeline

DOCX generation

python-docx installed but no template system

XLSX export

No XlsxWriter or openpyxl

Template management

No template CRUD, no template storage

Template designer UI

No visual template editor

Multi-format download

Artifact viewer only exports text blobs

Agent document tool

No generate_document tool for agents

Workflow document step

No document generation step in recipes

Document artifact kind

Artifact model lacks "document" kind

Architecture Assessment

Strengths we build on:

  • ReportGenerator already creates markdown reports agents can reference

  • Artifact model + viewer provides the delivery mechanism

  • PandasAI already generates charts that could embed in documents

  • python-docx already installed — just needs template layer

  • Document storage (local + S3) ready for generated files

Critical gap: There is no path from "agent has data" → "user gets a polished PDF/DOCX." Everything stops at Markdown or raw text.


3-Layer Architecture

New Dependencies

Backend (pip):

System (for WeasyPrint):

Frontend (npm) — Phase 7 only:


Part 4: Implementation Phases

Phase 1: Template Management System (4h)

Goal: CRUD for document templates with versioning

1.1 Database Schema

1.2 API Endpoints

1.3 Built-in Starter Templates

Ship with 5 pre-built templates:

Template
Format
Category
Variables

Basic Report

PDF

report

title, date, sections[], author

Invoice

PDF

invoice

company, client, line_items[], total, due_date

Meeting Notes

DOCX

report

title, date, attendees[], agenda[], action_items[]

Data Export

XLSX

data

title, columns[], rows[]

Executive Summary

PDF

report

title, date, highlights[], metrics{}, recommendations[]


Phase 2: PDF Generation Engine (4h)

Goal: Jinja2 + WeasyPrint pipeline for HTML/CSS → PDF

2.1 Service: DocumentGenerationService

File: orchestrator/modules/documents/generation_service.py

2.2 PDF Template System

Templates are HTML + CSS with Jinja2, stored in template_content:

2.3 Chart Embedding

Leverage existing PandasAI chart generation:


Phase 3: DOCX Generation Engine (3h)

Goal: python-docx-template for Word document output

3.1 Template Upload Flow

  1. User uploads .docx file with Jinja2 tags via /api/documents/templates/upload

  2. Backend extracts variable names from {{ }} tags

  3. Auto-generates data_schema from discovered variables

  4. Stores .docx in template storage path

  5. User can preview with sample data

3.2 DOCX Rendering


Phase 4: XLSX Export Engine (2h)

Goal: XlsxWriter for structured data export

4.1 Two Modes

Mode A: Data Export — NL2SQL results, database queries, any tabular data

Mode B: Template-based — Formatted reports with charts, multiple sheets, styling


Phase 5: Agent Tool Integration (3h)

Goal: Agents can generate documents via function calling

5.1 Agent Tool Schema

5.2 Integration in Agent Factory

File to modify: orchestrator/modules/agents/services/agent_platform_tools.py

Add generate_document alongside existing tools (search_codebase, query_database, etc.):

5.3 New Artifact Kind

Extend the Artifact model:

Frontend type update:


Phase 6: Workflow Document Step (2h)

Goal: Document generation as a recipe step type

6.1 New Step Type: generate_document

6.2 Example Recipe: Weekly Report


Phase 7: Frontend — Template Manager & Document Viewer (5h)

Goal: UI for managing templates, previewing documents, and downloading generated files

7.1 Template Manager Component

File: frontend/components/documents/template-manager.tsx

Features:

  • Grid view of templates with thumbnails, format badges, category filters

  • Create/edit template modal:

    • PDF: HTML/CSS code editor (Monaco) with live preview

    • DOCX: Upload .docx file, auto-detect variables, show schema

    • XLSX: Column/sheet configurator

  • Sample data editor (JSON) with "Preview" button

  • Template versioning (view history, rollback)

  • Duplicate/export template

7.2 Document Artifact Renderer

File: Update frontend/components/chatbot/artifact-viewer.tsx

Add 'document' kind handler:

7.3 Integration Points

  • Document Management tab: Add "Templates" sub-tab alongside existing Documents/CodeGraph tabs

  • Chat: Agent returns document artifacts inline with preview + download

  • Workflow builder: "Generate Document" step type in recipe editor with template picker and data mapper


Phase 8: Gotenberg Sidecar (2h) — Optional

Goal: DOCX/XLSX → PDF conversion via Docker sidecar

8.1 Docker Compose Addition

8.2 Conversion Service

8.3 Graceful Degradation

If Gotenberg is unavailable, fall back to:

  1. LibreOffice CLI (libreoffice --headless --convert-to pdf)

  2. Or simply return the native format with a "PDF conversion unavailable" message


File Change Summary

New Files

Backend:

File
Purpose

orchestrator/modules/documents/__init__.py

Module init

orchestrator/modules/documents/generation_service.py

Core generation engine

orchestrator/modules/documents/conversion_service.py

Gotenberg/LibreOffice conversion

orchestrator/modules/documents/template_service.py

Template CRUD operations

orchestrator/api/document_generation.py

REST API endpoints

orchestrator/alembic/versions/YYYYMMDD_add_document_templates.py

DB migration

orchestrator/modules/documents/templates/

Built-in starter templates (HTML/CSS)

Frontend:

File
Purpose

frontend/components/documents/template-manager.tsx

Template CRUD UI

frontend/components/documents/template-editor.tsx

HTML/CSS + preview editor

frontend/components/documents/template-picker.tsx

Template selection modal (for workflows)

Modified Files

File
Change

orchestrator/requirements.txt

Add weasyprint, docxtpl, xlsxwriter, gotenberg-client

orchestrator/core/models/core.py

Add DocumentTemplate model, update Artifact kind constraint

orchestrator/modules/agents/services/agent_platform_tools.py

Add generate_document tool

orchestrator/api/workflow_recipes.py

Add generate_document step type

frontend/types/chat.ts

Add 'document' to ArtifactKind

frontend/components/chatbot/artifact-viewer.tsx

Add document renderer

frontend/components/documents/document-management.tsx

Add Templates tab

docker-compose.yml

Add Gotenberg service (Phase 8)


Priority Matrix

Phase
What
Effort
Value
Priority

Phase 1

Template Management

4h

Foundation for everything

P0

Phase 2

PDF Generation (WeasyPrint)

4h

Highest-demand format

P0

Phase 5

Agent Tool Integration

3h

"Generate a report" in chat

P0

Phase 3

DOCX Generation

3h

Business document output

P1

Phase 4

XLSX Export

2h

Data export capability

P1

Phase 6

Workflow Document Step

2h

Automated report pipelines

P1

Phase 7

Frontend Template Manager

5h

Full self-service UI

P2

Phase 8

Gotenberg Sidecar

2h

Cross-format conversion

P2

MVP (Phases 1+2+5): 11h — Templates + PDF generation + agent integration Core (+ Phases 3+4+6): 18h — Add DOCX, XLSX, workflow step Full (+ Phases 7+8): 25h — Template designer UI + conversion sidecar


Success Criteria


User Stories

As a sales manager, I want to say "Generate this quarter's sales report as a PDF" and get a polished document with charts and metrics, so I can send it to leadership without manual formatting.

As an operations lead, I want a weekly recipe that automatically pulls Jira data, generates a status report, and emails it to stakeholders every Monday at 9am.

As a finance analyst, I want to export my NL2SQL query results as a formatted Excel spreadsheet with proper headers and number formatting.

As an admin, I want to upload our company's report template (branded DOCX) and have agents use it automatically when generating documents.


Integration with Existing PRDs

PRD
Integration Point

PRD-60 (RAG v3)

RAG query results → document sections. "Summarize our docs into a report"

PRD-61 (NL2SQL v2)

Query results → XLSX export or PDF table. "Export this query as Excel"

PRD-62 (CodeGraph v2)

Code analysis → technical documentation. "Generate API docs from codebase"

PRD-09 (Context Engineering)

Context data feeds document content

PRD-58 (Prompt Management)

System prompts for document generation style/tone


Out of Scope (Future)

  • Real-time collaborative document editing (Google Docs-style)

  • Presentation/slide generation (PPTX) — could add via python-pptx later

  • pdfme WYSIWYG frontend designer — revisit after core engine ships

  • Document signing / approval workflows

  • Batch document generation (mail merge for 1000+ documents)

  • Custom fonts/branding per workspace (use CSS for now)


Estimated Total Effort: MVP 11h | Core 18h | Full 25h Priority: P0 — Critical (enables the "AI → deliverable" pipeline) Dependencies: PRD-08 Document System (completed ✅), WeasyPrint system deps

Last updated