---
title: "Generate a PDF from HTML or Markdown"
method: POST
path: "/api/v1/generate-pdf"
tags: ["PDF"]
---

# Generate a PDF from HTML or Markdown

`POST /api/v1/generate-pdf`

Generates a PDF document from either Markdown content or raw HTML/CSS.
The generation mode is determined automatically: if the request body
contains `type: "html"` or the `data` property includes an `html` key,
HTML-to-PDF rendering is used; otherwise, Markdown-to-PDF rendering is used.

**Markdown mode** accepts structured content blocks (headings, labels,
body text, and tables) and renders them into a styled PDF with optional
logo, timestamp, and custom page dimensions.

**HTML mode** accepts raw HTML and optional CSS, rendering them directly
into a PDF with optional custom page dimensions.

Supports both JSON and multipart/form-data request bodies. Requires API
key (Basic auth) or OAuth authentication.

## Request body

- union
  - GenerateMarkdownPdfRequest — Generate a PDF from structured Markdown-style content blocks. Each item in the `data` array represents a content section that can contain headings, labeled values, body text, or tables.
    - `title` string — The document title. Used as the PDF metadata title and in the output filename.
    - `data` object[], required — An array of content blocks that make up the PDF body. Each block can contain one or more of: `heading`, `label`, `content`, `table`.
      - `heading` string — A section heading rendered in a larger, bold font.
      - `label` string — A label rendered in bold, typically used for key-value pairs.
      - `content` string — Body text content, rendered as a paragraph. Supports Markdown formatting.
      - `table` object — A table rendered inline in the PDF.
        - `rows` array[], required — Table data as a 2D array of strings (array of rows, each row is an array of cell values).
          - string[]
        - `columnOptions` object[] — Per-column display options.
          - `align` 'left' | 'center' | 'right' — Horizontal text alignment for this column.
          - `width` string — Column width as a percentage or pixel value (e.g. `25%`, `100px`).
        - `verticalAlign` 'top' | 'center' | 'bottom' — Vertical alignment of cell content.
        - `firstRowHeaders` boolean — When true, the first row is rendered as a header row with bold styling.
        - `rowGridlines` boolean — When true, horizontal gridlines are drawn between rows.
        - `columnGridlines` boolean — When true, vertical gridlines are drawn between columns.
      - `fontSize` integer — Font size override for this content block.
      - `textColor` string — Text color override for this block, as a CSS hex color.
      - `fontFamily` string — Font family override for this content block.
    - `textColor` string — Default text color for the document, as a CSS hex color (e.g. `#333333`).
    - `fontSize` integer — Default font size in points for the document.
    - `fontFamily` string — Default font family for the document.
    - `logo` union — A logo image to display at the top of the PDF. Set to `false` to explicitly disable any default logo. Provide an object with a base64-encoded or URL image source to include a logo.
      - false
      - object
        - `src` string, required — Image source: a base64-encoded data URI or an HTTPS URL.
        - `maxWidth` union — Maximum width of the logo in pixels.
          - string
          - number
        - `maxHeight` union — Maximum height of the logo in pixels.
          - string
          - number
    - `includeTimestamp` boolean — When true, a timestamp is printed on the PDF showing when it was generated.
    - `page` PageOptions — Custom page dimensions and margins for the generated PDF.
      - `width` string — Page width with unit (e.g. `8.5in`, `210mm`).
      - `height` string — Page height with unit (e.g. `11in`, `297mm`).
      - `margin` string — Shorthand margin for all sides (e.g. `0.5in`, `10mm 20mm`).
      - `marginTop` string — Top margin with unit.
      - `marginRight` string — Right margin with unit.
      - `marginBottom` string — Bottom margin with unit.
      - `marginLeft` string — Left margin with unit.
      - `pageCount` 'topLeft' | 'topRight' | 'topCenter' | 'bottomLeft' | 'bottomRight' | 'bottomCenter' — Position to render page numbers on each page.
    - `outputFormat` 'pdf' | 'url' — Controls the response format. When set to `url`, the generated PDF is uploaded to cloud storage and a JSON response with a short-lived signed URL is returned instead of the binary PDF. The signed URL expires 5 minutes after generation. Defaults to `pdf` (binary PDF response).
  - GenerateHtmlPdfRequest — Generate a PDF by rendering raw HTML and optional CSS.
    - `title` string — The document title. Used as the PDF metadata title and in the output filename.
    - `type` 'html' — Set to `html` to explicitly select HTML-to-PDF rendering.
    - `data` object, required — The HTML content and optional CSS to render into a PDF.
      - `html` string, required — The HTML markup to render. This is the full body content of the PDF.
      - `css` string — Optional CSS stylesheet applied to the HTML content during rendering.
    - `page` PageOptions — Custom page dimensions and margins for the generated PDF.
      - `width` string — Page width with unit (e.g. `8.5in`, `210mm`).
      - `height` string — Page height with unit (e.g. `11in`, `297mm`).
      - `margin` string — Shorthand margin for all sides (e.g. `0.5in`, `10mm 20mm`).
      - `marginTop` string — Top margin with unit.
      - `marginRight` string — Right margin with unit.
      - `marginBottom` string — Bottom margin with unit.
      - `marginLeft` string — Left margin with unit.
      - `pageCount` 'topLeft' | 'topRight' | 'topCenter' | 'bottomLeft' | 'bottomRight' | 'bottomCenter' — Position to render page numbers on each page.
    - `outputFormat` 'pdf' | 'url' — Controls the response format. When set to `url`, the generated PDF is uploaded to cloud storage and a JSON response with a short-lived signed URL is returned instead of the binary PDF. The signed URL expires 5 minutes after generation. Defaults to `pdf` (binary PDF response).

## Response `200`

The generated PDF document.

## Other responses

- `400` — Validation error — invalid or missing fields in the request body.
- `401` — Authentication required — API key or OAuth token is missing or invalid.
- `415` — Unsupported media type — Content-Type must be application/json or multipart/form-data.
- `429` — Concurrency limit exceeded — too many simultaneous requests on a development API key.

---

[API](https://skmtc.net/useanvil/apis/anvil-api.md) · [All operations](https://skmtc.net/useanvil/apis/anvil-api/llms.txt) · [OpenAPI document](https://skmtc-service-staging.skmtc.workers.dev/v1/apis/useanvil/anvil-api/revisions/ce258d1167ca/schema)
