# PDF documents

> Write an offer or an invoice as a markdown file with YAML frontmatter and print it as a letterhead PDF — totals computed, sender written once, German invoice rules checked

An offer or an invoice is a text file in git: the facts in YAML frontmatter, the covering letter in markdown. `@getnarro/pdf` turns it into a PDF with a letterhead, a logo, an address window, line items, VAT per rate and a footer on every page. You never type a total. You never repeat your own address, bank details or logo, because those live in one file the document pulls in.

## Quick start

Scaffold a sample, edit it, print it. No Narro deck is involved; the commands are `narro pdf`.

```bash
npx @getnarro/cli pdf init invoice
# edit company.yaml and invoice.md
npx @getnarro/cli pdf check invoice.md
npx @getnarro/cli pdf invoice.md
```

`pdf init <invoice|offer> [dir]` writes `company.yaml`, a placeholder `logo.svg`, a sample document and the schema mapping in `.vscode/settings.json`, merged into the file if you already have one. It refuses to overwrite an existing `company.yaml`, logo or sample. `pdf check` validates without starting a browser; `pdf invoice.md` writes `invoice.pdf` next to the input. Printing needs Chromium, the same browser `narro export --pdf` uses.

### The commands

`narro pdf <file> [-o, --out <file>] [--html]` prints the document. `--out` picks another output path; `--html` also writes the standalone HTML next to it, which is the quickest way to debug a layout.

`narro pdf check <file>` loads, validates and computes. It prints every problem at once, each with the file it came from and the field path, and exits 1:

```text
invoice.md › client.vatId: required when reverseCharge is true
```

When the document is valid it prints the net total, the VAT per rate and the gross total, and exits 0. Run it in CI or before every send.

## A document

The frontmatter holds the data; the body is free GitHub-flavoured markdown. Everything that is not the document itself comes from `extends:`.

```markdown
---
template: invoice
extends: ./company.yaml
number: RE-2026-0042
date: 2026-10-26
serviceDate: 2026-10-20
paymentTerms: 14
client:
  name: ACME GmbH
  address: [Musterstraße 1, 10115 Berlin]
items:
  - { description: Narro Pro licence, qty: 3, unit: Seat, price: 49.00, vat: 19 }
  - { description: Onboarding workshop, qty: 1, price: 800, vat: 19 }
discount: { percent: 5 }
---
Thank you for your order.

<!-- items -->

Payable within 14 days without deduction.
```

Without the `<!-- items -->` marker the whole body is printed above the items table and the totals. With it, the table and totals appear exactly there. The marker may appear once.

Raw HTML in the body is dropped, not rendered. A markdown image must be a local `.svg`, `.png` or `.jpg` file, and it is inlined; a remote image is rejected. The logo, the fonts and every image are embedded in the PDF, so it looks the same on every machine.

Render only documents you trust, never an untrusted upload. A document can reference any local file through `extends`, `logo`, `css` and body images, and any template module, which runs as code. The stylesheet named by `css` can also load remote resources through `@import` or `url()`.

## The shared sender file

Your name, address, tax IDs, bank, logo and theme belong in one `company.yaml`. Every document names it with `extends:`, and the file may extend another one in turn.

```yaml
locale: de-DE
currency: EUR
logo: ./logo.svg
logoWidth: 38mm
sender:
  name: Narro GmbH
  address: [Beispielweg 5, 80331 München]
  vatId: DE999999999
bank:
  iban: DE02 1203 0000 0000 2020 51
theme:
  accent: "#4F46E5"
  font: Inter
```

The client's country is printed in the address window only when it differs from yours, as DIN 5008 asks for mail abroad. Your country is `sender.country`, or else the region of `locale`, so `de-DE` makes it Germany.

### How extends merges

A chain resolves depth first: later files override earlier ones, and the document overrides all of them. Objects merge key by key. Arrays and scalars replace, so `items` can never merge by accident. Paths in `extends`, `logo`, `css` and `template` resolve relative to the file that declares them, not to the document you print.

A cycle, a missing file or a YAML root that is not a mapping is an error that names the chain. Because every merged value remembers its file, a validation error tells you which file to fix. A document cannot currently unset an inherited value; to leave something out, leave it out of the shared file.

### Company fields

These are the fields a `company.yaml` can set. A document can set any of them too.

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `locale` | `string` | — | BCP 47 locale for dates, numbers and labels. Default `de-DE`. |
| `currency` | `string` | — | ISO 4217 currency code. Default `EUR`. |
| `paper` | `"A4" \| "Letter"` | — | Paper size. Default `A4`. |
| `letterhead` | `"din5008-a" \| "din5008-b" \| "minimal"` | — | Page layout: DIN 5008 form A or B, or a minimal letterhead. Default `din5008-b`. |
| `logo` | `string` | — | Path to an svg, png or jpg logo, relative to the file declaring it |
| `logoWidth` | `string` | — | Printed logo width, e.g. 42mm |
| `sender` | `object` | — | Who sends the document |
| `sender.name` | `string` | — | Legal name of the sender |
| `sender.address` | `string[]` | — | Address lines of the sender |
| `sender.country` | `string` | — | Two-letter country code of the sender; defaults to the locale's region (de-DE: DE) |
| `sender.email` | `string` | — | Contact email, printed in the footer |
| `sender.phone` | `string` | — | Phone number, printed in the footer |
| `sender.website` | `string` | — | Website, printed in the footer |
| `sender.vatId` | `string` | — | VAT ID; an invoice needs this or taxNumber, and this under reverseCharge |
| `sender.taxNumber` | `string` | — | Tax number; an invoice needs this or vatId |
| `sender.register` | `string` | — | Commercial register entry, e.g. Amtsgericht München, HRB 123456 |
| `sender.managingDirectors` | `string[]` | — | Managing directors, printed in the footer |
| `bank` | `object` | — | Bank details, printed in the footer |
| `bank.name` | `string` | — | Name of the bank |
| `bank.iban` | `string` | yes (if `bank` is set) | IBAN payments go to |
| `bank.bic` | `string` | — | BIC of the bank |
| `theme` | `object` | — | Accent colour and font |
| `theme.accent` | `string` | — | Accent colour for headings, the table header rule and totals, as a hex colour. Default `#4F46E5`. |
| `theme.font` | `"Inter" \| "Source Serif 4" \| "IBM Plex Sans"` | — | Bundled font family. Default `Inter`. |
| `css` | `string` | — | Path to a stylesheet appended last, relative to the file declaring it |

## Items and computed totals

Each item has a description, a quantity, a net price and a VAT rate. Prices are read as exact decimals and held as integer cents: more than two decimals is a validation error. Quantities may be fractional, such as 1.5 hours, and a price may be negative for a credit.

The line total is the quantity times the price, rounded to the cent. A `discount` is a percentage or a fixed amount and applies to the net before VAT. It is spread over the VAT rates pro rata, so the VAT base stays correct for each rate; leftover cents go to the largest group. VAT is computed once per rate on the summed net of that rate, rounded half up. The gross total is the net, less the discount, plus the VAT. The due date is `date` plus `paymentTerms` days unless you give `dueDate`; an offer's `validDays` works the same way. The grand total of an invoice may not be negative, and a fixed discount may not exceed the net total.

## German rules

The invoice template checks the content §14 UStG requires, so a missing piece fails `pdf check` and not your client's accountant. An invoice needs a number, a date, the sender's name and address, the client's name and address, a VAT ID or a tax number, a service date or a service period, and at least one item.

Two VAT modes change the document:

- `reverseCharge: true` requires `client.vatId` and, on an invoice, `sender.vatId` (§14a UStG; a tax number is not enough). It requires every item's VAT to be 0 and prints the reverse-charge note.
- `smallBusiness: true` applies §19 UStG: every item's VAT must be 0 and the note is printed.

Setting both is an error. With a German locale (`de` or `de-DE`) the VAT rate of an item must be 0, 7 or 19; other locales accept any rate from 0 to 100. An offer needs a number, a date, `validUntil` or `validDays`, the parties and at least one item; `acceptance: true` adds a signature and date block.

## Layout and theme

`letterhead` chooses the page: `din5008-a` and `din5008-b` place the address window and the meta block at the DIN 5008 positions for form A or B, and `minimal` is a plain letterhead. `paper` is `A4` or `Letter`; fold and punch marks print on A4 only. Page two onwards gets a slim header, and every page gets a three-column footer with address and contact, bank details, and tax IDs, register and directors, plus "Page X of Y". Long item tables flow onto further pages with the header row repeated and no split rows.

`theme.accent` colours headings, the table header rule and the totals. `theme.font` is one of the bundled families, so nothing is fetched at print time. For anything else, point `css` at a stylesheet that is appended last; the theme is a layer of CSS custom properties you can override.

## Invoice and offer fields

Both templates take the company fields above plus their own. This is the full list for an invoice.

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `locale` | `string` | — | BCP 47 locale for dates, numbers and labels. Default `de-DE`. |
| `currency` | `string` | — | ISO 4217 currency code. Default `EUR`. |
| `paper` | `"A4" \| "Letter"` | — | Paper size. Default `A4`. |
| `letterhead` | `"din5008-a" \| "din5008-b" \| "minimal"` | — | Page layout: DIN 5008 form A or B, or a minimal letterhead. Default `din5008-b`. |
| `logo` | `string` | — | Path to an svg, png or jpg logo, relative to the file declaring it |
| `logoWidth` | `string` | — | Printed logo width, e.g. 42mm |
| `sender` | `object` | yes | Who sends the document |
| `sender.name` | `string` | yes | Legal name of the sender |
| `sender.address` | `string[]` | yes | Address lines of the sender |
| `sender.country` | `string` | — | Two-letter country code of the sender; defaults to the locale's region (de-DE: DE) |
| `sender.email` | `string` | — | Contact email, printed in the footer |
| `sender.phone` | `string` | — | Phone number, printed in the footer |
| `sender.website` | `string` | — | Website, printed in the footer |
| `sender.vatId` | `string` | — | VAT ID; an invoice needs this or taxNumber, and this under reverseCharge |
| `sender.taxNumber` | `string` | — | Tax number; an invoice needs this or vatId |
| `sender.register` | `string` | — | Commercial register entry, e.g. Amtsgericht München, HRB 123456 |
| `sender.managingDirectors` | `string[]` | — | Managing directors, printed in the footer |
| `bank` | `object` | — | Bank details, printed in the footer |
| `bank.name` | `string` | — | Name of the bank |
| `bank.iban` | `string` | yes (if `bank` is set) | IBAN payments go to |
| `bank.bic` | `string` | — | BIC of the bank |
| `theme` | `object` | — | Accent colour and font |
| `theme.accent` | `string` | — | Accent colour for headings, the table header rule and totals, as a hex colour. Default `#4F46E5`. |
| `theme.font` | `"Inter" \| "Source Serif 4" \| "IBM Plex Sans"` | — | Bundled font family. Default `Inter`. |
| `css` | `string` | — | Path to a stylesheet appended last, relative to the file declaring it |
| `template` | `string` | — | Built-in template name, or a path to a custom template module |
| `number` | `string \| number` | yes | Document number |
| `date` | `string \| date` | yes | Document date, as YYYY-MM-DD |
| `client` | `object` | yes | Who the document is addressed to |
| `client.name` | `string` | yes | Company or person name, the first line of the address window |
| `client.contact` | `string` | — | Contact person, printed under the name |
| `client.address` | `string[]` | yes | Address lines, at most five besides the name (contact included) |
| `client.country` | `string` | — | Two-letter country code, printed when it differs from sender.country (or the locale's region) |
| `client.vatId` | `string` | — | The client's VAT ID; required with reverseCharge |
| `client.customerNumber` | `string \| number` | — | Your customer number for the client |
| `items` | `object[]` | yes | Line items; totals are computed from them |
| `items[].description` | `string` | yes | What is being charged |
| `items[].details` | `string` | — | Extra lines under the description |
| `items[].qty` | `number` | yes | Quantity; may be fractional, e.g. 1.5 |
| `items[].unit` | `string` | — | Unit of the quantity, e.g. Std. |
| `items[].price` | `number \| string` | yes | Net price per unit, at most two decimals; negative for a credit |
| `items[].vat` | `number` | yes | VAT rate in percent; for German locales 0, 7 or 19 |
| `discount` | `object` | — | Discount applied to the net before VAT |
| `discount.percent` | `number` | — | Percentage off the net total; give this or amount |
| `discount.amount` | `number \| string` | — | Fixed amount off the net total; give this or percent |
| `reverseCharge` | `boolean` | — | Reverse charge: VAT 0 and the legal note; needs client.vatId. Default `false`. |
| `smallBusiness` | `boolean` | — | Small-business rule (§ 19 UStG): VAT 0 and the legal note. Default `false`. |
| `subject` | `string` | — | Subject line; defaults to the document type and number |
| `serviceDate` | `string \| date` | — | Date of supply; an invoice needs this or servicePeriod |
| `servicePeriod` | `object` | — | Period of supply, from and to; an invoice needs this or serviceDate |
| `servicePeriod.from` | `string \| date` | yes (if `servicePeriod` is set) | First day of the period |
| `servicePeriod.to` | `string \| date` | yes (if `servicePeriod` is set) | Last day of the period |
| `paymentTerms` | `integer` | — | Days after date until payment is due |
| `dueDate` | `string \| date` | — | Due date, instead of paymentTerms |

And for an offer.

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `locale` | `string` | — | BCP 47 locale for dates, numbers and labels. Default `de-DE`. |
| `currency` | `string` | — | ISO 4217 currency code. Default `EUR`. |
| `paper` | `"A4" \| "Letter"` | — | Paper size. Default `A4`. |
| `letterhead` | `"din5008-a" \| "din5008-b" \| "minimal"` | — | Page layout: DIN 5008 form A or B, or a minimal letterhead. Default `din5008-b`. |
| `logo` | `string` | — | Path to an svg, png or jpg logo, relative to the file declaring it |
| `logoWidth` | `string` | — | Printed logo width, e.g. 42mm |
| `sender` | `object` | yes | Who sends the document |
| `sender.name` | `string` | yes | Legal name of the sender |
| `sender.address` | `string[]` | yes | Address lines of the sender |
| `sender.country` | `string` | — | Two-letter country code of the sender; defaults to the locale's region (de-DE: DE) |
| `sender.email` | `string` | — | Contact email, printed in the footer |
| `sender.phone` | `string` | — | Phone number, printed in the footer |
| `sender.website` | `string` | — | Website, printed in the footer |
| `sender.vatId` | `string` | — | VAT ID; an invoice needs this or taxNumber, and this under reverseCharge |
| `sender.taxNumber` | `string` | — | Tax number; an invoice needs this or vatId |
| `sender.register` | `string` | — | Commercial register entry, e.g. Amtsgericht München, HRB 123456 |
| `sender.managingDirectors` | `string[]` | — | Managing directors, printed in the footer |
| `bank` | `object` | — | Bank details, printed in the footer |
| `bank.name` | `string` | — | Name of the bank |
| `bank.iban` | `string` | yes (if `bank` is set) | IBAN payments go to |
| `bank.bic` | `string` | — | BIC of the bank |
| `theme` | `object` | — | Accent colour and font |
| `theme.accent` | `string` | — | Accent colour for headings, the table header rule and totals, as a hex colour. Default `#4F46E5`. |
| `theme.font` | `"Inter" \| "Source Serif 4" \| "IBM Plex Sans"` | — | Bundled font family. Default `Inter`. |
| `css` | `string` | — | Path to a stylesheet appended last, relative to the file declaring it |
| `template` | `string` | — | Built-in template name, or a path to a custom template module |
| `number` | `string \| number` | yes | Document number |
| `date` | `string \| date` | yes | Document date, as YYYY-MM-DD |
| `client` | `object` | yes | Who the document is addressed to |
| `client.name` | `string` | yes | Company or person name, the first line of the address window |
| `client.contact` | `string` | — | Contact person, printed under the name |
| `client.address` | `string[]` | yes | Address lines, at most five besides the name (contact included) |
| `client.country` | `string` | — | Two-letter country code, printed when it differs from sender.country (or the locale's region) |
| `client.vatId` | `string` | — | The client's VAT ID; required with reverseCharge |
| `client.customerNumber` | `string \| number` | — | Your customer number for the client |
| `items` | `object[]` | yes | Line items; totals are computed from them |
| `items[].description` | `string` | yes | What is being charged |
| `items[].details` | `string` | — | Extra lines under the description |
| `items[].qty` | `number` | yes | Quantity; may be fractional, e.g. 1.5 |
| `items[].unit` | `string` | — | Unit of the quantity, e.g. Std. |
| `items[].price` | `number \| string` | yes | Net price per unit, at most two decimals; negative for a credit |
| `items[].vat` | `number` | yes | VAT rate in percent; for German locales 0, 7 or 19 |
| `discount` | `object` | — | Discount applied to the net before VAT |
| `discount.percent` | `number` | — | Percentage off the net total; give this or amount |
| `discount.amount` | `number \| string` | — | Fixed amount off the net total; give this or percent |
| `reverseCharge` | `boolean` | — | Reverse charge: VAT 0 and the legal note; needs client.vatId. Default `false`. |
| `smallBusiness` | `boolean` | — | Small-business rule (§ 19 UStG): VAT 0 and the legal note. Default `false`. |
| `subject` | `string` | — | Subject line; defaults to the document type and number |
| `validUntil` | `string \| date` | — | Last day the offer is valid; an offer needs this or validDays |
| `validDays` | `integer` | — | Days after date the offer stays valid |
| `acceptance` | `boolean` | — | Adds a signature and date block for the client. Default `false`. |

## Custom templates

The built-in templates are `invoice`, `offer`. Set `template:` to a built-in name, or to the path of a local ES module whose default export comes from `defineTemplate`:

```ts
import { defineTemplate, ItemsTable, Letterhead, offerSchema } from "@getnarro/pdf";
import { createElement as h, Fragment } from "react";

export default defineTemplate({
  name: "quote",
  schema: offerSchema, // or your own zod schema built on the same fields
  title: ({ data }) => `Quote ${data.number}`,
  render: (ctx) =>
    h(
      Fragment,
      null,
      h(Letterhead, {
        ctx,
        documentTitle: `Quote ${ctx.data.number}`,
        meta: [["Quote", ctx.data.number]],
      }),
      h(ItemsTable, { ctx }),
    ),
  css: "",
});
```

`Letterhead` draws the first page, `ItemsTable` the items with their totals. The package also exports `AddressWindow`, `MetaBlock`, `TotalsBlock` and `PageFooter`, so a custom template keeps the letterhead and replaces only what differs. A custom template is trusted code: it runs in Node with your permissions when you print, so only point `template:` at files you wrote or reviewed.

## Editor autocomplete

The JSON Schemas for `company.yaml` and each template ship in the package under `schema/`, generated from the validator itself so an editor cannot suggest a key `pdf check` rejects. `pdf init` writes a `# yaml-language-server: $schema=` comment into `company.yaml` and a `.vscode/settings.json` entry that maps the documents to the schemas, so completion and hover text work in VS Code with the YAML extension straight away.

## From code

The same pipeline is a library. `renderHtml(file)` loads, validates and computes, and returns `{ html, title, paper, data, totals }`. `renderPdf(rendered, { browser })` prints that to PDF bytes; pass your own Playwright browser or let it launch Chromium.

```ts
import { writeFile } from "node:fs/promises";
import { renderHtml, renderPdf } from "@getnarro/pdf";

const rendered = await renderHtml("invoice.md");
await writeFile("invoice.pdf", await renderPdf(rendered));
```

`renderPdf` needs `playwright-core`, an optional peer dependency, and a Chromium it can find.