---
name: codepdf
description: >-
  Create, edit, preview, and share production PDF templates, data models, Draft
  2020-12 JSON Schemas, and runnable PDF generation code across 22 libraries in
  8 runtimes using the focused 3-tool CodePDF MCP server (create_template,
  edit_template, share_template) or HTTP plugin API. Use when the user asks to
  create an invoice, receipt, resume, certificate, report, proposal, letter,
  custom PDF layout, edit an existing .pdftemplate or existing PDF code, or
  share a PDF preview link.
---

# CodePDF — 3-Tool PDF Template, Schema, Codegen & Share Skill

Build production PDF templates, inferred data models, Draft 2020-12 JSON Schemas, and runnable standalone code across **22 PDF libraries in 8 runtimes** using CodePDF's 3 focused MCP tools: **`create_template`**, **`edit_template`**, and **`share_template`**.

## 1. Choose Execution Channel

1. **If CodePDF MCP tools are mounted** (`https://mcp.codepdf.dev`, `https://codepdf.dev/api/mcp`, or local stdio `bin/codepdf-mcp.mjs`):
   - Call `create_template`, `edit_template`, or `share_template` directly.
   - Read `codepdf://templates/catalog` or `codepdf://libraries/matrix` resources only if you need to browse gallery slugs or compare all 22 libraries.
2. **If MCP tools are NOT mounted** (shell/HTTP fallback):
   - Use [scripts/invoke-codepdf.mjs](./scripts/invoke-codepdf.mjs):
     ```bash
     node ./scripts/invoke-codepdf.mjs tool create_template --args '{"documentType":"invoice","title":"SaaS Invoice","techStack":{"runtime":"Node.js","framework":"react"}}'
     node ./scripts/invoke-codepdf.mjs templates --query invoice --limit 5
     node ./scripts/invoke-codepdf.mjs libraries --runtime py
     ```

## 2. Creating a New Template (`create_template`)

Follow these steps in **1 tool call**:
1. **Base the template & PDF code on the current project's codebase:**
   - Inspect the current project first: manifests (`package.json`, `pyproject.toml`, `requirements.txt`, `composer.json`, `Cargo.toml`, `go.mod`, `Gemfile`, `pom.xml`, `*.csproj`), `tsconfig.json`/module format (TypeScript vs. plain JavaScript, ESM vs. CommonJS), lockfiles/package manager, existing domain models/types/schemas, existing PDF helpers/libraries, brand styling, and existing test directory/framework.
   - Pass `library` (if explicitly requested) OR `techStack` + `projectContext` (`usesTypeScript`, `moduleFormat`, `packageManager`, `existingTemplateDir`, `testDirectory`, `testFramework`, `detectedPdfLibrariesInCode`, `referenceFiles`, `codingConventions`). Local stdio MCP auto-detects these from project manifests and source files.
2. **Determine the use case** (`documentType` or built-in gallery `templateSlug`) and **separate static vs. dynamic values**:
   - Keep boilerplate, section headings, labels, and column headers **static**.
   - Express dynamic values using `{{var}}` tokens and pass structured `fields` + `table`/`tables` aligned with the project's real data models so CodePDF builds `dataModel`, `sampleData`, and a Draft 2020-12 `jsonSchema`.
   - Escape literal template syntax in prose as `\{{var}}` or set `disableBindings: true`.
3. **Structure template code cleanly into ONE file or ONE folder** via `codeStructure`:
   - **`codeStructure: "file"` (default):** Emit ONE self-contained template code file kept in the **exact same folder as the `.pdftemplate`** (e.g., `templates/saas-invoice.pdftemplate` + `templates/saas-invoice.tsx`, or `src/pdf/invoice.pdftemplate` + `src/pdf/invoice.js`).
   - **`codeStructure: "folder"`:** Group the `.pdftemplate`, entry code file, and any helper/style/type files for that template inside ONE dedicated per-template folder (e.g., `templates/saas-invoice/`).
4. **Keep PDF tests in a separate folder:**
   - If tests are written for PDF generation, ALWAYS put them in a **separate folder** reported in `projectLayout.testsFolder` (e.g., `tests/pdf/` or `<detectedTestDir>/pdf/`, never inside the template/`.pdftemplate` folder), with any test output PDFs in `projectLayout.testOutputFolder` (e.g., `tests/pdf/output/`).
   - Optionally pass `testsFolder` or `includeTests: true` to scaffold a smoke test directly into that separate test folder.
5. **Save BOTH `.pdftemplate` and generated code (and integrate cleanly):**
   - **Local stdio MCP:** Both files (`savedTemplate.relativePath` and `savedCodeFile.relativePath`, plus `savedTestFile.relativePath` if `includeTests: true`) are saved automatically.
   - **Remote HTTP MCP:** Decode `pdftemplateFile.contentBase64` (base64) to `pdftemplateFile.suggestedPath`, write `code` to `codeFile.suggestedPath`, and (if `testFile` is returned) write `testFile.code` to `testFile.suggestedPath`. Keep `@codepdf-template:` intact when adapting exports/types to integrate with existing project code.
6. **Present the result cleanly:** Show `installCommand`, `jsonSchema` / dynamic fields summary, and the saved file paths (including the separate `projectLayout.testsFolder` if tests are written).
7. **Never upload automatically:** Ask the user *"Do you want to see a preview of the generated PDF or share this template with others?"* and call `share_template` **only** if they say yes.

## 3. Editing an Existing Template (`edit_template`)

When asked to **edit, update, or enhance** a PDF template:

- **Path A — `.pdftemplate` exists in the project:**
  1. Check the project for an existing `*.pdftemplate` file (or check the `@codepdf-template:` comment at the top of the PDF code file).
  2. Call `edit_template` with `templatePath` (stdio) or `existingPdftemplate` base64 (HTTP), `projectContext` (so updated code stays aligned with the current project's TS/JS & ESM/CJS conventions), and only the incremental changes (`title`, `accentColor`, `kpis`, `fields`, `sections`, `table`/`tables`, `removeSectionHeadings`, `removeComponentIds`, surgical AST `edits`, `library`, `compress`, `fontFamily`).
  3. Need exact component IDs before surgical edits? Call `edit_template` with `inspectOnly: true` first to decrypt and inspect `outline`, `dataModel`, and `jsonSchema` without modifying disk.
  4. Save both updated files (`.pdftemplate` and regenerated code in the same folder or dedicated template folder) back to the project (automatic in stdio), and keep any PDF tests in the separate `projectLayout.testsFolder`.

- **Path B — No `.pdftemplate`, but existing PDF generation code exists:**
  1. Read the user's existing PDF code file (`sourceCodePath`).
  2. Extract its layout, static vs. dynamic values, tables, and styling, merge the user's requested edits, and call `edit_template` with `sourceMode: "from_existing_code"`, `sourceCodePath`, `existingCode` (or `library`/`techStack`/`projectContext`), and the extracted template spec.
  3. CodePDF generates a canonical `.pdftemplate` in the **same folder** as `sourceCodePath` (or in a dedicated folder when `codeStructure: "folder"`), `dataModel`, `jsonSchema`, and clean regenerated code tagged with `@codepdf-template` matching the current project — save both files so all future edits use **Path A**. Keep any PDF tests in the separate `projectLayout.testsFolder`. Offer a preview via `share_template` to verify visual parity.

## 4. Sharing or Previewing (`share_template`)

Only call `share_template` **after** the user explicitly asks to preview or share:
- Live preview: `share_template` with `mode: "preview"` (1–7 day TTL, opens browser in local stdio).
- Team sharing: `share_template` with `mode: "share"` (1–30 day TTL, optional custom `passcode` min 12 chars).
- Always display the exact returned `previewUrl` / `shareUrl` and separate `password` — **never** embed passwords in URLs.

## 5. Reference Docs

- Full 3-tool schema, resources, prompts & examples: [references/tools-and-schema.md](./references/tools-and-schema.md)
- All 22 supported PDF libraries across 8 runtimes: [references/libraries-and-runtimes.md](./references/libraries-and-runtimes.md)
