# Convert to PDF

> Generic string-to-PDF converter.

Source: https://docs.sommatic.ai/en/nodes/data.convert.to.pdf/

- Slug: `data.convert.to.pdf`
- Family: Data
- Version: 1.0.0
- Status: Available
- Execution: sync

Generic string-to-PDF converter. Accepts plain text, Markdown, or HTML and produces a PDF binary encoded as base64 in the envelope (Link Loom envelopes do not transport raw Buffers between nodes).

**Supported input types**
- `text` — rendered as-is, plain wrap.
- `markdown` — parsed with `marked`; renders headings, paragraphs, bold/italic/code, ordered/unordered lists, blockquotes, links, horizontal rules.
- `html` — parsed with `htmlparser2`; renders `h1..h6`, `p`, `strong/b`, `em/i`, `code/pre`, `ul/ol/li`, `blockquote`, `a`, `hr`, `br`. Non-supported tags render as text. Images (`<img>`) are dropped — carry them as separate attachments if needed.

**Page decoration (all optional)**
- `metadata` — object rendered as a key/value block above the body (e.g. From/To/Subject/Date for an email; Contract/Client/Date for a document).
- `document_header` — text painted at the top of every page (e.g. institutional header).
- `document_footer` — text painted at the bottom of every page (e.g. legal footer).
- `watermark` — rotated background text with configurable opacity.
- `page_numbers` — appends "Página N de M" to the bottom-right of every page.

**Design constraints**
- Pure input → output. No blob download, no external I/O, no Cosmos/Mongo touch. Fully testable in isolation.
- Reusable for any `string → PDF` scenario (email body, contracts, manuals, logs). Not coupled to any specific domain.
- Uses pdfkit (JS pure, no chromium). Fast and lightweight; suitable for envelopes.

## Configuration

- `content` (string, required): The source content (plain text, Markdown, or HTML) to convert into a PDF.
- `input_type` (string, optional): How to interpret the content string. Default: `text`. Allowed values: `text`, `markdown`, `html`.
- `filename` (string, optional): Logical filename for the produced PDF (used in metadata and echoed in output). Default: `document.pdf`.
- `page_size` (string, optional): Default: `A4`. Allowed values: `A4`, `Letter`.
- `page_margin` (number, optional): Uniform margin in points (72pt = 1in). Default: `40`.
- `metadata` (object, optional): Key/value pairs rendered as a small table above the body.
- `document_header` (object, optional): Text painted at the top of every page.
  - `text` (string, optional)
  - `style` (object, optional)
    - `font_size` (number, optional): Font size in points.
    - `bold` (boolean, optional): Whether the text is bold.
    - `color` (string, optional): Text color as hex (e.g. "#666666").
    - `align` (string, optional): Horizontal alignment. Allowed values: `left`, `center`, `right`.
- `document_footer` (object, optional): Text painted at the bottom of every page (e.g. legal disclaimer).
  - `text` (string, optional)
  - `style` (object, optional)
    - `font_size` (number, optional): Font size in points.
    - `bold` (boolean, optional): Whether the text is bold.
    - `color` (string, optional): Text color as hex (e.g. "#666666").
    - `align` (string, optional): Horizontal alignment. Allowed values: `left`, `center`, `right`.
- `watermark` (object, optional): Rotated background text painted on every page.
  - `text` (string, optional)
  - `opacity` (number, optional): Opacity 0..1 (default 0.06).
  - `rotation` (number, optional): Rotation in degrees (default 45).
  - `color` (string, optional): Hex color (default "#000000").
  - `font_size` (number, optional): Watermark font size (default 60pt).
- `page_numbers` (boolean, optional): If true, prints "Página N de M" on the bottom-right of every page. Default: `false`.

Its configuration shows the shared execution settings, but this node does not apply retries (`retryOnFail`, `maxTries`) yet. `continueOnFail` is not applied by any node.

## Output ports

- `out` (default): PDF generated successfully.
- `error`: Conversion failed.

## Input contract

- `content` (string, required)
- `input_type` (string, optional)

## Output contract

- `pdf_base64` (string, optional): PDF binary base64-encoded (fits Link Loom envelope constraints).
- `filename` (string, optional)
- `size_bytes` (number, optional): Raw PDF size in bytes (pre-base64).
- `input_type` (string, optional): Echo of the input_type used.
- `format` (string, optional): Always 'pdf'.

## In expressions

What a later node can read from this node's output with `$("Convert to PDF").item.json.<path>` (write your node's label instead of `Convert to PDF`). **Editor** marks the paths the workflow editor suggests under Previous Nodes (the output contract); **Worker** marks the paths the worker really returns through `out`, from the output shown in Ports.

- `$("Convert to PDF").item.json.pdf_base64` (string) = `"JVBERi0xLjMKJf////8KNyAwIG9iago8PAovVHlwZSAv…"`: PDF binary base64-encoded (fits Link Loom envelope constraints).
- `$("Convert to PDF").item.json.filename` (string) = `"case-summary.pdf"`
- `$("Convert to PDF").item.json.size_bytes` (number) = `18432`: Raw PDF size in bytes (pre-base64).
- `$("Convert to PDF").item.json.input_type` (string) = `"markdown"`: Echo of the input_type used.
- `$("Convert to PDF").item.json.format` (string) = `"pdf"`: Always 'pdf'.


## How it works

Renders plain text, Markdown or HTML into a PDF with PDFKit and returns it base64-encoded, with an optional title, data blocks, page header, footer, watermark and page numbers.

1. Resolves the config. `content` comes from the config, or from `content` in the incoming payload; an empty value fails the node.
2. Reads the layout: `input_type` (default `text`, case-insensitive), `filename` (default `document.pdf`), `page_size` (default `A4`) and `page_margin` (default 50 points). A header adds 30 points to the top margin; a footer or page numbers add 30 points to the bottom margin.
3. Draws the optional blocks before the body, in this order: `title` (with `subtitle`), `traceability` and `metadata`. When any of them is present, a fixed bold label precedes the body.
4. Draws the body: `text` as is; `markdown` through the marked lexer (headings, paragraphs, bold, italic, inline code, links, lists, code blocks, blockquotes, rules); `html` through htmlparser2 (`h1`–`h6`, `p`, `pre`, `blockquote`, `li`, `hr`, `br`, `b`/`strong`, `i`/`em`, `code`, `a`).
5. Paints on every page the watermark, `document_header`, `document_footer` and, with `page_numbers`, a bottom-right page counter.
6. Returns `{ pdf_base64, filename, size_bytes, input_type, format: "pdf" }` through `out`.

## In a workflow

### Node

```json
{
  "node_id": "node-render-summary",
  "label": "Render summary PDF",
  "operator_slug": "data.convert.to.pdf",
  "operator_version": "1.0.0",
  "config": {
    "content": "{{ $json.summary_markdown }}",
    "input_type": "markdown",
    "filename": "case-summary.pdf",
    "page_size": "A4",
    "page_numbers": true,
    "document_footer": {
      "text": "Confidential. Generated by an automated workflow."
    }
  }
}
```

### Envelope

```json
{
  "ok": true,
  "data": {
    "pdf_base64": "JVBERi0xLjMKJf////8KNyAwIG9iago8PAovVHlwZSAvUGFnZQo...",
    "filename": "case-summary.pdf",
    "size_bytes": 18432,
    "input_type": "markdown",
    "format": "pdf"
  },
  "port": "out"
}
```

## Output ports (behaviour in the code)

### `out`

The PDF was generated. `size_bytes` is the raw PDF size before base64.

```json
{
  "pdf_base64": "JVBERi0xLjMKJf////8KNyAwIG9iago8PAovVHlwZSAvUGFnZQo...",
  "filename": "case-summary.pdf",
  "size_bytes": 18432,
  "input_type": "markdown",
  "format": "pdf"
}
```

### `error`

`content` is empty or PDFKit failed.

```json
{
  "error": "content is required"
}
```

## Behaviour in the code

- `page_margin`: The code default is 50 points, not the 40 shown in the schema. Non-numeric values fall back to 50.
- `title`: Not declared. Centered bold title (14 pt) with an optional `subtitle` (also undeclared) and a rule below.
- `traceability`: Not declared. Object of key/value pairs drawn as a small monospaced block; null or empty values are skipped. `metadata` works the same way with a regular font.
- `watermark`: Code defaults: `opacity` 0.06, `rotation` -30 degrees, `font_size` 40, `color` `#000000` (the schema text says 45 degrees and 60 pt).
- `document_header`: `text` plus `style` (`align`, `color`, `font_size` default 8, `bold`). Long text is cut with an ellipsis: one line in the header, two lines in `document_footer`.

## Errors

| Message | Port | Cause and fix |
| --- | --- | --- |
| `content is required` | `error` | No `content` in the config or in the payload. Map it, for example `{{ $json.summary_markdown }}`. |
| `Cannot read properties of undefined (reading '0')` | `error` | `page_size` is not a size PDFKit knows. Use `A4` or `Letter`. |

## Notes and limits

- Uses the PDF standard fonts Helvetica and Courier: characters outside their Latin set (for example Chinese, Japanese or emoji) are not rendered correctly.
- Fixed labels are in Spanish: "Trazabilidad", "Encabezados del correo", "Contenido del correo" and "Página N de M".
- Images are ignored, Markdown tables are not drawn and HTML tables are flattened into plain paragraphs.
- The whole PDF is held in memory and travels base64-encoded in the payload (about 4/3 of its size). The incoming payload is replaced by the result.
- The worker does not set `ok` (the example shows `true`); the engine routes only by `port`.


## Contracts (JSON Schema)

### All (`*.definition.js`)

```json
{
  "slug": "data.convert.to.pdf",
  "name": "Convert to PDF",
  "category": "data",
  "version": "1.0.0",
  "description": "Generic string-to-PDF converter. Accepts plain text, Markdown, or HTML and produces a PDF binary encoded as base64 in the envelope (Link Loom envelopes do not transport raw Buffers between nodes).\n\n**Supported input types**\n- `text` — rendered as-is, plain wrap.\n- `markdown` — parsed with `marked`; renders headings, paragraphs, bold/italic/code, ordered/unordered lists, blockquotes, links, horizontal rules.\n- `html` — parsed with `htmlparser2`; renders `h1..h6`, `p`, `strong/b`, `em/i`, `code/pre`, `ul/ol/li`, `blockquote`, `a`, `hr`, `br`. Non-supported tags render as text. Images (`<img>`) are dropped — carry them as separate attachments if needed.\n\n**Page decoration (all optional)**\n- `metadata` — object rendered as a key/value block above the body (e.g. From/To/Subject/Date for an email; Contract/Client/Date for a document).\n- `document_header` — text painted at the top of every page (e.g. institutional header).\n- `document_footer` — text painted at the bottom of every page (e.g. legal footer).\n- `watermark` — rotated background text with configurable opacity.\n- `page_numbers` — appends \"Página N de M\" to the bottom-right of every page.\n\n**Design constraints**\n- Pure input → output. No blob download, no external I/O, no Cosmos/Mongo touch. Fully testable in isolation.\n- Reusable for any `string → PDF` scenario (email body, contracts, manuals, logs). Not coupled to any specific domain.\n- Uses pdfkit (JS pure, no chromium). Fast and lightweight; suitable for envelopes.",
  "capabilities": [
    "pdf",
    "convert",
    "html-to-pdf",
    "markdown-to-pdf",
    "text-to-pdf"
  ],
  "config_schema": {
    "type": "object",
    "required": [
      "content"
    ],
    "properties": {
      "content": {
        "type": "string",
        "title": "Content",
        "format": "textarea",
        "description": "The source content (plain text, Markdown, or HTML) to convert into a PDF."
      },
      "input_type": {
        "type": "string",
        "enum": [
          "text",
          "markdown",
          "html"
        ],
        "default": "text",
        "title": "Input Type",
        "description": "How to interpret the content string."
      },
      "filename": {
        "type": "string",
        "default": "document.pdf",
        "title": "Filename",
        "description": "Logical filename for the produced PDF (used in metadata and echoed in output)."
      },
      "page_size": {
        "type": "string",
        "enum": [
          "A4",
          "Letter"
        ],
        "default": "A4",
        "title": "Page Size"
      },
      "page_margin": {
        "type": "number",
        "default": 40,
        "title": "Page Margin (points)",
        "description": "Uniform margin in points (72pt = 1in)."
      },
      "metadata": {
        "type": "object",
        "title": "Metadata Block",
        "description": "Key/value pairs rendered as a small table above the body.",
        "additionalProperties": {
          "type": "string"
        }
      },
      "document_header": {
        "type": "object",
        "title": "Page Header",
        "description": "Text painted at the top of every page.",
        "properties": {
          "text": {
            "type": "string"
          },
          "style": {
            "type": "object",
            "properties": {
              "font_size": {
                "type": "number",
                "description": "Font size in points."
              },
              "bold": {
                "type": "boolean",
                "description": "Whether the text is bold."
              },
              "color": {
                "type": "string",
                "description": "Text color as hex (e.g. \"#666666\")."
              },
              "align": {
                "type": "string",
                "enum": [
                  "left",
                  "center",
                  "right"
                ],
                "description": "Horizontal alignment."
              }
            }
          }
        }
      },
      "document_footer": {
        "type": "object",
        "title": "Page Footer",
        "description": "Text painted at the bottom of every page (e.g. legal disclaimer).",
        "properties": {
          "text": {
            "type": "string"
          },
          "style": {
            "type": "object",
            "properties": {
              "font_size": {
                "type": "number",
                "description": "Font size in points."
              },
              "bold": {
                "type": "boolean",
                "description": "Whether the text is bold."
              },
              "color": {
                "type": "string",
                "description": "Text color as hex (e.g. \"#666666\")."
              },
              "align": {
                "type": "string",
                "enum": [
                  "left",
                  "center",
                  "right"
                ],
                "description": "Horizontal alignment."
              }
            }
          }
        }
      },
      "watermark": {
        "type": "object",
        "title": "Watermark",
        "description": "Rotated background text painted on every page.",
        "properties": {
          "text": {
            "type": "string"
          },
          "opacity": {
            "type": "number",
            "description": "Opacity 0..1 (default 0.06)."
          },
          "rotation": {
            "type": "number",
            "description": "Rotation in degrees (default 45)."
          },
          "color": {
            "type": "string",
            "description": "Hex color (default \"#000000\")."
          },
          "font_size": {
            "type": "number",
            "description": "Watermark font size (default 60pt)."
          }
        }
      },
      "page_numbers": {
        "type": "boolean",
        "default": false,
        "title": "Show Page Numbers",
        "description": "If true, prints \"Página N de M\" on the bottom-right of every page."
      },
      "execution": {
        "type": "object",
        "title": "Execution Settings",
        "properties": {
          "alwaysOutputData": {
            "type": "boolean",
            "title": "Always Output Data",
            "description": "Always output data, even on error (if configured).",
            "default": false
          },
          "retryOnFail": {
            "type": "boolean",
            "title": "Retry On Fail",
            "description": "Retry the execution if it fails.",
            "default": false
          },
          "maxTries": {
            "type": "integer",
            "title": "Max Retries",
            "description": "Number of retries.",
            "default": 3,
            "displayOptions": {
              "show": {
                "retryOnFail": [
                  true
                ]
              }
            }
          },
          "waitBetweenTries": {
            "type": "integer",
            "title": "Wait Between Retries (ms)",
            "default": 1000,
            "displayOptions": {
              "show": {
                "retryOnFail": [
                  true
                ]
              }
            }
          },
          "continueOnFail": {
            "type": "boolean",
            "title": "Continue On Fail",
            "description": "Do not crash workflow on error.",
            "default": false
          },
          "timeout_ms": {
            "type": "integer",
            "title": "Timeout (ms)",
            "description": "Max execution time for this node before it is killed, in milliseconds. Honored by long-running operators (e.g. OCR/extraction, inline code). Default 300000 (5 min).",
            "default": 300000
          }
        }
      }
    }
  },
  "input_contract_schema": {
    "type": "object",
    "required": [
      "content"
    ],
    "properties": {
      "content": {
        "type": "string"
      },
      "input_type": {
        "type": "string"
      }
    }
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "pdf_base64": {
        "type": "string",
        "description": "PDF binary base64-encoded (fits Link Loom envelope constraints)."
      },
      "filename": {
        "type": "string"
      },
      "size_bytes": {
        "type": "number",
        "description": "Raw PDF size in bytes (pre-base64)."
      },
      "input_type": {
        "type": "string",
        "description": "Echo of the input_type used."
      },
      "format": {
        "type": "string",
        "description": "Always 'pdf'."
      }
    }
  },
  "ports": [
    {
      "port_id": "out",
      "label": "PDF",
      "description": "PDF generated successfully.",
      "is_default": true,
      "supports_condition": true
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Conversion failed.",
      "is_default": false,
      "supports_condition": false
    }
  ],
  "execution_mode": "sync",
  "required_resources": [],
  "tags": [
    "pdf",
    "convert",
    "html",
    "markdown",
    "text",
    "render"
  ],
  "is_stateful": false,
  "publisher": {
    "identity": "org_link_loom",
    "profile": {
      "name": "Link Loom",
      "verified": true,
      "url": "https://me.veripass.com/link-loom",
      "logo_url": null
    }
  }
}
```

### config_schema

```json
{
  "type": "object",
  "required": [
    "content"
  ],
  "properties": {
    "content": {
      "type": "string",
      "title": "Content",
      "format": "textarea",
      "description": "The source content (plain text, Markdown, or HTML) to convert into a PDF."
    },
    "input_type": {
      "type": "string",
      "enum": [
        "text",
        "markdown",
        "html"
      ],
      "default": "text",
      "title": "Input Type",
      "description": "How to interpret the content string."
    },
    "filename": {
      "type": "string",
      "default": "document.pdf",
      "title": "Filename",
      "description": "Logical filename for the produced PDF (used in metadata and echoed in output)."
    },
    "page_size": {
      "type": "string",
      "enum": [
        "A4",
        "Letter"
      ],
      "default": "A4",
      "title": "Page Size"
    },
    "page_margin": {
      "type": "number",
      "default": 40,
      "title": "Page Margin (points)",
      "description": "Uniform margin in points (72pt = 1in)."
    },
    "metadata": {
      "type": "object",
      "title": "Metadata Block",
      "description": "Key/value pairs rendered as a small table above the body.",
      "additionalProperties": {
        "type": "string"
      }
    },
    "document_header": {
      "type": "object",
      "title": "Page Header",
      "description": "Text painted at the top of every page.",
      "properties": {
        "text": {
          "type": "string"
        },
        "style": {
          "type": "object",
          "properties": {
            "font_size": {
              "type": "number",
              "description": "Font size in points."
            },
            "bold": {
              "type": "boolean",
              "description": "Whether the text is bold."
            },
            "color": {
              "type": "string",
              "description": "Text color as hex (e.g. \"#666666\")."
            },
            "align": {
              "type": "string",
              "enum": [
                "left",
                "center",
                "right"
              ],
              "description": "Horizontal alignment."
            }
          }
        }
      }
    },
    "document_footer": {
      "type": "object",
      "title": "Page Footer",
      "description": "Text painted at the bottom of every page (e.g. legal disclaimer).",
      "properties": {
        "text": {
          "type": "string"
        },
        "style": {
          "type": "object",
          "properties": {
            "font_size": {
              "type": "number",
              "description": "Font size in points."
            },
            "bold": {
              "type": "boolean",
              "description": "Whether the text is bold."
            },
            "color": {
              "type": "string",
              "description": "Text color as hex (e.g. \"#666666\")."
            },
            "align": {
              "type": "string",
              "enum": [
                "left",
                "center",
                "right"
              ],
              "description": "Horizontal alignment."
            }
          }
        }
      }
    },
    "watermark": {
      "type": "object",
      "title": "Watermark",
      "description": "Rotated background text painted on every page.",
      "properties": {
        "text": {
          "type": "string"
        },
        "opacity": {
          "type": "number",
          "description": "Opacity 0..1 (default 0.06)."
        },
        "rotation": {
          "type": "number",
          "description": "Rotation in degrees (default 45)."
        },
        "color": {
          "type": "string",
          "description": "Hex color (default \"#000000\")."
        },
        "font_size": {
          "type": "number",
          "description": "Watermark font size (default 60pt)."
        }
      }
    },
    "page_numbers": {
      "type": "boolean",
      "default": false,
      "title": "Show Page Numbers",
      "description": "If true, prints \"Página N de M\" on the bottom-right of every page."
    },
    "execution": {
      "type": "object",
      "title": "Execution Settings",
      "properties": {
        "alwaysOutputData": {
          "type": "boolean",
          "title": "Always Output Data",
          "description": "Always output data, even on error (if configured).",
          "default": false
        },
        "retryOnFail": {
          "type": "boolean",
          "title": "Retry On Fail",
          "description": "Retry the execution if it fails.",
          "default": false
        },
        "maxTries": {
          "type": "integer",
          "title": "Max Retries",
          "description": "Number of retries.",
          "default": 3,
          "displayOptions": {
            "show": {
              "retryOnFail": [
                true
              ]
            }
          }
        },
        "waitBetweenTries": {
          "type": "integer",
          "title": "Wait Between Retries (ms)",
          "default": 1000,
          "displayOptions": {
            "show": {
              "retryOnFail": [
                true
              ]
            }
          }
        },
        "continueOnFail": {
          "type": "boolean",
          "title": "Continue On Fail",
          "description": "Do not crash workflow on error.",
          "default": false
        },
        "timeout_ms": {
          "type": "integer",
          "title": "Timeout (ms)",
          "description": "Max execution time for this node before it is killed, in milliseconds. Honored by long-running operators (e.g. OCR/extraction, inline code). Default 300000 (5 min).",
          "default": 300000
        }
      }
    }
  }
}
```

### input_contract_schema

```json
{
  "type": "object",
  "required": [
    "content"
  ],
  "properties": {
    "content": {
      "type": "string"
    },
    "input_type": {
      "type": "string"
    }
  }
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "pdf_base64": {
      "type": "string",
      "description": "PDF binary base64-encoded (fits Link Loom envelope constraints)."
    },
    "filename": {
      "type": "string"
    },
    "size_bytes": {
      "type": "number",
      "description": "Raw PDF size in bytes (pre-base64)."
    },
    "input_type": {
      "type": "string",
      "description": "Echo of the input_type used."
    },
    "format": {
      "type": "string",
      "description": "Always 'pdf'."
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "out",
    "label": "PDF",
    "description": "PDF generated successfully.",
    "is_default": true,
    "supports_condition": true
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "Conversion failed.",
    "is_default": false,
    "supports_condition": false
  }
]
```
