# PDF Forge & Layout Diagnostics Edge API (v1.0.0)

> Compile Markdown or HTML into polished multi-page PDF 1.7 documents with orphan-page & wide-table diagnostics in <20ms.

Zero-dependency Markdown & HTML to spec-compliant PDF 1.7 compiler with corporate themes ('executive', 'invoice', 'technical', 'memo'), multi-page table header continuation, diagonal watermarks, deterministic layout diagnostics (orphan pages, wide tables, broken images), and binary PDF inspection.

## Why Agents Use This API Instead of DIY
Eliminates heavyweight headless Chromium containers, broken PDF byte-offset xref tables, clipped wide tables, and embarrassing 2-line trailing orphan pages before delivering client-facing PDFs.

## Connection & Payment (x402 Default + Optional Bearer API Key)
- **MCP Server (Streamable HTTP):** `https://pdf-forge.apikeg.com/mcp` (Registry ID: `com.apikeg/pdf-forge` — `initialize`, `tools/list`, `resources/*`, and `prompts/*` are free)
- **OpenAPI 3.1 Spec:** [openapi.json](https://pdf-forge.apikeg.com/openapi.json)
- **Full Schema & Examples:** [llms-full.txt](https://pdf-forge.apikeg.com/llms-full.txt)
- **RFC 9727 API Catalog:** [.well-known/api-catalog](https://pdf-forge.apikeg.com/.well-known/api-catalog)
- **x402 Bazaar Manifest:** [.well-known/x402.json](https://pdf-forge.apikeg.com/.well-known/x402.json)
- **MCP Discovery Manifest:** [.well-known/mcp.json](https://pdf-forge.apikeg.com/.well-known/mcp.json)
- **A2A Agent Card:** [.well-known/agent-card.json](https://pdf-forge.apikeg.com/.well-known/agent-card.json)
- **Primary Payment (Zero Signup):** Cloudflare Monetization Gateway (`HTTP 402` / `x402` v2 with USDC on Base).
- **Optional Monthly API Key:** Pass `Authorization: Bearer ak_live_...` ($5 / $20 / $50 monthly per-product Stripe plans at [apikeg.com/account](https://apikeg.com/account)).
- **Free Preflight:** Append `?dry_run=true` (or header `X-Dry-Run: true`) to validate any request payload at `$0.00` cost.

## Tools & REST Endpoints
- [`render_document_pdf`](https://pdf-forge.apikeg.com/tools/render-document-pdf.md) (`POST https://pdf-forge.apikeg.com/v1/render-document-pdf`) — $0.004 USD per rendered PDF document (x402 instant micropayment): Compiles Markdown (headings, bold/italic/code, lists, GFM tables, callouts, '---pagebreak---') or HTML into both print-ready themed HTML and a spec-compliant multi-page PDF 1.7 binary with running headers, 'Page X of Y' footers, repeated table headers across page breaks, optional watermark, and deterministic layoutDiagnostics.
- [`inspect_pdf_document`](https://pdf-forge.apikeg.com/tools/inspect-pdf-document.md) (`POST https://pdf-forge.apikeg.com/v1/inspect-pdf-document`) — $0.001 USD per PDF binary inspection (x402 instant micropayment): Parses a Base64-encoded PDF binary to verify %PDF header/EOF integrity and extract pdfVersion, exact pageCount, byteLength, encryption status, AcroForm presence, and Info dictionary metadata.

## Instant MCP Configuration
```json
{
  "mcpServers": {
    "pdf-forge": {
      "type": "streamable-http",
      "url": "https://pdf-forge.apikeg.com/mcp"
    }
  }
}
```

## Common Agent Use Cases
- convert markdown to PDF API for AI agents
- generate executive report or invoice PDF MCP tool
- detect trailing orphan page and wide table clipping in PDF
- pure edge HTML markdown to PDF 1.7 generator x402
- inspect PDF page count metadata encryption API

---

## Detailed Tool Specifications

### `render_document_pdf` — Render Markdown or HTML Document to Themed PDF
- **REST Endpoint:** `POST https://pdf-forge.apikeg.com/v1/render-document-pdf`
- **Tool Doc Page:** https://pdf-forge.apikeg.com/tools/render-document-pdf
- **MCP Tool Name:** `render_document_pdf`
- **Pricing:** $0.004 USD per rendered PDF document (x402 instant micropayment)
- **Side Effects:** readOnly=true, idempotent=true, destructive=false
- **Build vs. Buy ROI:** Avoids spinning up 400MB Puppeteer/Playwright containers or hand-rolling brittle PDF stream/xref math; returns both binary PDF and layout QA diagnostics in <20ms at $0.004.

#### Input JSON Schema
```json
{
  "type": "object",
  "properties": {
    "content": {
      "type": "string",
      "minLength": 1,
      "maxLength": 524288,
      "description": "Markdown (headings, bold/italic/code, lists, GFM tables, callouts, '---pagebreak---') or raw HTML body to compile."
    },
    "contentFormat": {
      "type": "string",
      "enum": [
        "markdown",
        "html"
      ],
      "default": "markdown",
      "description": "Input syntax format ('markdown' or 'html')."
    },
    "title": {
      "type": "string",
      "maxLength": 240,
      "description": "Optional document title rendered in the cover banner and stored in PDF /Title metadata."
    },
    "subtitle": {
      "type": "string",
      "maxLength": 320,
      "description": "Optional subtitle rendered below the title and stored in PDF /Subject metadata."
    },
    "author": {
      "type": "string",
      "maxLength": 160,
      "description": "Optional author or organization name rendered in the cover banner and PDF /Author metadata."
    },
    "theme": {
      "type": "string",
      "enum": [
        "executive",
        "invoice",
        "technical",
        "memo"
      ],
      "default": "executive",
      "description": "Corporate print styling theme ('executive', 'invoice', 'technical', or 'memo')."
    },
    "pageSize": {
      "type": "string",
      "enum": [
        "Letter",
        "A4",
        "Legal"
      ],
      "default": "Letter",
      "description": "Paper size ('Letter', 'A4', or 'Legal')."
    },
    "orientation": {
      "type": "string",
      "enum": [
        "portrait",
        "landscape"
      ],
      "default": "portrait",
      "description": "Page orientation ('portrait' or 'landscape')."
    },
    "headerText": {
      "type": "string",
      "maxLength": 180,
      "description": "Optional running header stamped at the top margin of every page."
    },
    "includePageNumbers": {
      "type": "boolean",
      "default": true,
      "description": "Include 'Page X of Y' running footers on every page."
    },
    "watermarkText": {
      "type": "string",
      "maxLength": 80,
      "description": "Optional diagonal watermark text rendered across every page (e.g. 'CONFIDENTIAL' or 'DRAFT')."
    }
  },
  "required": [
    "content"
  ],
  "additionalProperties": false
}
```

#### Examples
**Render Q4 Executive Board Report with Financials Table** — Compiles Markdown with GFM tables, callouts, running headers, and CONFIDENTIAL watermark into a Letter PDF.
```bash
curl -X POST "https://pdf-forge.apikeg.com/v1/render-document-pdf" \
  -H "Content-Type: application/json" \
  -d '{"title":"Q4 Cloud FinOps Executive Summary","subtitle":"Quarterly Infrastructure Margin & Savings Audit","author":"Platform FinOps Team","theme":"executive","pageSize":"Letter","orientation":"portrait","contentFormat":"markdown","headerText":"ACME CORP — Q4 FINOPS AUDIT","includePageNumbers":true,"watermarkText":"CONFIDENTIAL","content":"## Highlights\n\nNet cloud margin improved **420 bps** quarter-over-quarter.\n\n| Service | Q3 Spend | Q4 Spend | Delta |\n| --- | ---: | ---: | ---: |\n| Compute | $142,000 | $118,500 | -16.5% |\n| Storage | $38,400 | $35,100 | -8.6% |\n\n> [!NOTE]\n> Reserved capacity commitments lock in an additional $95k annualized savings."}'
```
Expected result: Base64 PDF 1.7 binary, print-ready themed HTML, byteLength, and layoutDiagnostics (pageCount, lastPageFillRatio, wideTableWarnings, estimatedReadingMinutes).

### `inspect_pdf_document` — Inspect Binary PDF Structure & Metadata
- **REST Endpoint:** `POST https://pdf-forge.apikeg.com/v1/inspect-pdf-document`
- **Tool Doc Page:** https://pdf-forge.apikeg.com/tools/inspect-pdf-document
- **MCP Tool Name:** `inspect_pdf_document`
- **Pricing:** $0.001 USD per PDF binary inspection (x402 instant micropayment)
- **Side Effects:** readOnly=true, idempotent=true, destructive=false
- **Build vs. Buy ROI:** Instant deterministic PDF preflight without installing native poppler/pdfinfo binaries; $0.001 per check.

#### Input JSON Schema
```json
{
  "type": "object",
  "properties": {
    "pdfBase64": {
      "type": "string",
      "minLength": 8,
      "maxLength": 5242880,
      "description": "Base64-encoded PDF binary (standard or URL-safe Base64, optional 'data:application/pdf;base64,' prefix)."
    }
  },
  "required": [
    "pdfBase64"
  ],
  "additionalProperties": false
}
```

#### Examples
**Verify compiled PDF page count and metadata**
```bash
curl -X POST "https://pdf-forge.apikeg.com/v1/inspect-pdf-document" \
  -H "Content-Type: application/json" \
  -d '{"pdfBase64":"JVBERi0xLjcK..."}'
```
Expected result: validPdf=true, pdfVersion='1.7', pageCount, byteLength, encrypted=false, hasAcroForm=false, and metadata.
