# outputd Operator Guide

> Operator guide for outputd, the customer-communications daemon: operator-owned Typst templates, the ZUGFeRD PDF/A-3 carrier and per-channel delivery evidence.

Source: https://hupe1980.github.io/mako/docs/services/outputd/

`outputd` renders the documents a customer receives and delivers them. It owns
**how documents look** — the operator's Typst templates, the render engine, the
ZUGFeRD PDF/A-3 carrier, the publish gates and the append-only template store —
and **whether they arrived**: the store of issued documents and the per-channel
delivery evidence. What a document **says** — amounts, VAT, legal basis — stays
with the issuing service: billingd for invoices, accountingd for the Mahnwesen
figures, vertragd for a price change. outputd never recomputes a number.

One brand has one template store, so a logo change reaches the invoice *and*
the Mahnung.

| Document | What it is | Form |
|---|---|---|
| **Mahnung** | dunning letter, computed by [accountingd](https://hupe1980.github.io/mako/docs/services/accountingd/) | Textform (§ 126b BGB: a readable declaration on a durable medium naming sender and recipient; no signature) |
| **Preisanpassung** | the price-change notice of § 41 Abs. 5 EnWG | Textform |
| **Gebührenbescheid** | a public-law Gebühr from billingd, issued beside the supply invoice (§ 157 Abs. 1 AO content) | letter, no carrier |
| **Vertragszusammenfassung** | the summary of § 41 Abs. 4 EnWG every Letztverbraucher is owed after conclusion, from vertragd | Textform |
| **Vertragsbestätigung** | the § 312f Abs. 2 BGB confirmation of a consumer's Fernabsatz contract with the Widerrufsbelehrung and the Muster-Widerrufsformular, from vertragd | Textform |
| **Widerrufsbestätigung** | the § 356a Abs. 4 BGB Eingangsbestätigung of a Widerruf, from vertragd | Textform |
| **Invoice** | from billingd | **ZUGFeRD**: a PDF/A-3 carrying the EN 16931 invoice XML; **XRechnung** is the German CIUS. The document declares which in **BT-24**, and outputd stamps the carrier from that |

Port: `:9880`

The OpenAPI document at `GET /api/v1/openapi.json` describes the delivery-evidence lookup by document kind and issuer reference.

| Method | Path | Description |
|---|---|---|
| `POST` | `/api/v1/render/{kind}` | Render a view with the current or a pinned template; `X-Mako-Template-Hash` names the layout used. Stores nothing |
| `POST` | `/api/v1/documents/issue/{kind}` | Render, **record** and queue for delivery; idempotent on `subject_ref` |
| `GET` | `/api/v1/documents` | A customer's documents (`?malo_id=` or `?kunden_nr=`, `&kind=`) — the portal inbox |
| `GET` | `/api/v1/documents/{id}` | One document with every delivery track |
| `GET` | `/api/v1/documents/{id}/content` | The bytes as issued — a reproduction, never a re-render |
| `GET` | `/api/v1/documents/by-subject/{kind}?subject_ref=…` | Channel states and timestamps for one reference, including invoice numbers with slashes. The response contains the kind, the reference and the delivery tracks’ states and times. |
| `GET` | `/api/v1/documents/by-subject/{kind}/{subject_ref}` | The document a `(kind, subject_ref)` was issued as, with every delivery track — how vertragd reads a Vertragsbestätigung's `delivered_at` |
| `GET` | `/api/v1/deliveries?status=FAILED&kind=` | Every delivery that ended `FAILED`, with its document's `kind` and `subject_ref` — the operator's work list |
| `POST` | `/api/v1/deliveries/{id}/read` | The customer opened it in the portal |
| `POST` | `/api/v1/deliveries/{id}/status` | A channel reports arrival or a bounce |
| `GET` | `/api/v1/spool` | What a print service collects |
| `GET` | `/api/v1/templates` | Every template this tenant published (`?kind=&limit=`) |
| `POST` | `/api/v1/templates` | Prove a template, then store it forever |
| `POST` | `/api/v1/templates/preview` | Render an `INVOICE`/`MAHNUNG` candidate against its specimen; stores nothing |
| `GET` | `/api/v1/templates/reference/{kind}` | The reference layout mako ships, one per kind |
| `GET`/`PUT` | `/api/v1/templates/{kind}/current` | Which template is rolled out |
| `GET` | `/api/v1/templates/by-hash/{hash}` | Resolve the layout an issued document used |

## The render API — `POST /api/v1/render/{kind}`

The caller sends what the document is *about*, optionally a **template hash**
(a pinned document; omitted, the tenant's current template renders), and — for
`INVOICE` — the **CII payload with its BT-24**, so the ZUGFeRD carrier is
stamped exactly as the document declares itself. The answer is the PDF, with
`X-Mako-Template-Hash` naming the template used; the issuing service pins that
hash next to its record.

What "about" means depends on the kind:

| Kind | Field | Why |
|---|---|---|
| `INVOICE` | `model` — the EN 16931 semantic model | outputd projects the page view from it, so the projection the publish gate proves templates against is the one production feeds them |
| every other kind | `view` — the kind's own view | their producer has no EN 16931 model, and the view *is* the contract |

An `INVOICE` may carry `nachrechnen_url` — billingd's public Nachrechnen link
for the record. outputd adds it to the view as `nachrechnen` (`url`, and
`qr_svg`, the same link as a QR code at error correction level M), which the
reference layout prints under the totals; an `http(s)` link only
(**`422 INVALID_NACHRECHNEN_URL`**), and on no other kind
(**`422 NACHRECHNEN_ONLY_ON_AN_INVOICE`**). It is not an EN 16931 term, so the
embedded CII does not carry it.

A Textform letter is written in the deployment's
[`[policy] anrede`](#policy-anrede): outputd adds `anrede` (`"sie"` or `"du"`)
to the view before its template renders it, and answers with
`X-Mako-Anrede`. The producer does not send one — a view carrying `anrede` is
**`422 ANREDE_IN_VIEW`**. A re-print pins the form an issued document recorded
with the request's top-level `anrede`, the way `template_hash` pins its layout;
a value that is neither `sie` nor `du` is **`422 ANREDE_UNKNOWN`**, and `du` on a
`GEBUEHRENBESCHEID` is **`422 ANREDE_FIXED`**.

`POST /api/v1/documents/issue/{kind}` takes the same body, without `anrede`,
plus `subject_ref`, `recipient` and `channels`. The stored document records the
`anrede` it was written in.

An `INVOICE` sent by `POST` alone is a sonstige Rechnung, which § 14 Abs. 2
Satz 2 Nr. 1 UStG does not admit for a Leistung „an einen anderen Unternehmer
für dessen Unternehmen" outside the graces of § 27 Abs. 38 UStG. The request
states `empfaenger_unternehmer`; for an Unternehmer also `leistung_ausgefuehrt`
(the supply's last day) and, where Nr. 2 decides, `gesamtumsatz_vorjahr_eur`.
outputd decides as billingd's paper path does, with today's Berlin date as the
übermittelt date:

| Supply executed | Paper passes when | Grace |
|---|---|---|
| 2025–2026 | übermittelt by 31.12.2026 | § 27 Abs. 38 Nr. 1 |
| 2027 | übermittelt by 31.12.2027 and the issuer's 2026 Gesamtumsatz „nicht mehr als 800 000 Euro" | Nr. 2 |

Otherwise — or where a fact is missing — it answers
**`422 PAPIER_UNZULAESSIG`**. A `POST` beside `PORTAL`, `EMAIL` or `ERP` is a
copy of a document that also went out electronically and is queued.

An `INVOICE` is issued only to a buyer in Germany. It is the operator's energy
invoice — gas, electricity or heating/cooling, taxed as a domestic supply — so
the buyer country (BT-55) decides:

| BT-55 | Answer |
|---|---|
| another EU Member State | **`422 CROSS_BORDER_SUPPLY_UNSUPPORTED`** — the OSS treatment under Art. 369aa MwStSystRL is not built |
| outside the EU | **`422 EXPORT_SUPPLY_UNSUPPORTED`** |
| absent | **`422 RECIPIENT_COUNTRY_MISSING`** — EN 16931 BR-11 makes it mandatory, and outputd does not read it as `DE` |
| not two letters | **`422 RECIPIENT_COUNTRY_INVALID`** |

The postal `recipient.address` is where a
letter goes, not who is invoiced, and does not decide. billingd asks the same
rule before it stores an invoice; `POST /api/v1/render/{kind}` issues nothing
and does not ask it.

Admission is checked before rendering, each refusal a `422` naming the fix: a
pinned hash must belong to **this kind**, the subject must match the kind, an
`INVOICE` render **requires** the attachment, and a Textform render must not
carry one. A hash of another tenant does not resolve (`template_store::by_hash`
is tenant-scoped).

## Authorization

`policies/outputd.cedar` decides every route before it touches the database:

| Action | Routes | Who |
|---|---|---|
| `read-template` | `GET /templates`, `/{kind}/current`, `/by-hash/{hash}`, `/reference/{kind}` | any authenticated caller in the tenant |
| `preview-template` | `POST /templates/preview` | any authenticated caller in the tenant |
| `read-policy`, `read-config` | `GET /api/v1/policy`, `GET /admin/einrichtung` — the operator policy and the configuration findings | any authenticated caller in the tenant |
| `publish-template` | `POST /templates` | `LF`, `MSB`, `ESA` |
| `rollout-template` | `PUT /templates/{kind}/current` | `LF`, `MSB`, `ESA` |
| `render-document` | `POST /render/{kind}` | `LF`, `MSB`, `ESA` |
| `issue-document` | `POST /documents/issue/{kind}` | `LF`, `MSB`, `ESA` |
| `report-delivery` | `POST /deliveries/{id}/status` | `LF`, `MSB`, `ESA` |
| `read-document` | `GET /documents…` (`/by-subject/{kind}/{subject_ref}` included), `/spool`, `POST /deliveries/{id}/read` | any authenticated caller in the tenant |
| `read-delivery-failures` | `GET /deliveries?status=FAILED` | `LF`, `MSB`, `ESA` — the list is tenant-wide, not scoped by its query |

A template is the shape of every document, so publishing, rolling out and
rendering need a market role. `tests/it/authorization.rs` holds handler checks
and policy grants to each other in both directions. Reading is a tenant read
because the **query** scopes it: `GET /api/v1/documents` refuses to answer
without `malo_id` or `kunden_nr` (`UNSCOPED_QUERY`). `portald` turns a
customer's identity into that scope via `vertragd`.

## Issuing and delivery

`POST /render/{kind}` keeps nothing (preview, re-print, a caller with its own
archive). `POST /documents/issue/{kind}` records and queues, which answers two
regulated questions:

- **Reproduce what was issued.** § 14 Abs. 1 Satz 2 UStG and § 147 Abs. 1
  Nr. 2–3 AO keep the Rechnungsdoppel eight years in the form issued;
  `GET /documents/{id}/content` returns the stored bytes, never a re-render.
- **Did the customer receive it.** § 126b BGB and § 41f Abs. 1/Abs. 5 EnWG rest
  on notices that *reached* the customer.

Both stores are append-only; a corrected document is a new row.

### Idempotency

`subject_ref` names the subject in the issuer's terms (Rechnungsnummer,
dunning-case id, product-slice id) and is unique per `(tenant, kind)`, so a
retry gets the document already issued — essential where a duplicate Mahnung
would be a second statutory demand.

### Channels

| Channel | How outputd delivers | Evidence | `DELIVERED` when |
|---|---|---|---|
| `PORTAL` | the document is in the store; `portald` serves it | published, then `read_at` when the customer opens it | on publish — it is then in the recipient's sphere, which is what § 126b BGB asks of a durable medium |
| `EMAIL` | `POST` to a configured mail relay | the relay's message id | the relay reports the recipient's server accepted it |
| `POST` | a spool a print service pulls (`GET /api/v1/spool`); an optional push when `postal_relay_url` is set | the batch reference it reports back | the service reports it posted |
| `ERP` | `POST` to the operator's own webhook | its response | the ERP reports it |

outputd embeds **no SMTP client and no print driver**: each outbound channel is
an HTTP relay (URL, bearer token, JSON with the document base64-encoded, signed
Standard Webhooks). With none configured, the portal channel still delivers.

A channel with nothing to send to (`EMAIL` with no address) is stored
`SUPPRESSED` **with its reason**. `recipient.address` is typed and
all-or-nothing — street, postcode and town — and an absent address suppresses
`POST`. `SENT` (relay accepted) and `DELIVERED` (far end reported via
`POST /deliveries/{id}/status`) are different states; a reported bounce turns
even a `DELIVERED` row `FAILED` and clears `delivered_at`.

### Delivery

Issuing a document commits, in the same transaction, the publication of a
`PORTAL` delivery (recorded `DELIVERED`) and one `event_outbox` command per
pushed channel, keyed `de.output.delivery/<delivery_id>` — the Standard Webhooks
`webhook-id`, so a retry is the same message. A relay worker per configured
channel delivers them under the shared outbox policy (8 attempts, 30 s × 4ⁿ
capped at 2 h); the relay's receipt is written as the evidence in the
transaction that marks the command delivered. A delivery's `attempts` and
`next_attempt_at` are read off its command. One the outbox gave up on becomes
`FAILED` with the last error and is logged at `error`. With
`[delivery] enabled = false` nothing is set going and rows stay `PENDING`.

A `POST` delivery without `postal_relay_url` is **not pushed**: in the pull
model the print service reads `GET /api/v1/spool`, fetches the bytes and reports
back, so the row stays `PENDING` in the spool until collected. Setting
`postal_relay_url` switches new letters to push.

## Errors

Every route answers with one envelope and a stable code. Template compile errors
come back as a **list** of `path:line:col: message` diagnostics pointing into the
operator's file:

```json
{ "error": { "code": "TEMPLATE_DID_NOT_COMPILE",
             "message": "the template did not render",
             "diagnostics": ["/template.typ:12:4: unknown variable: invoce"] } }
```

| Code | Status | Meaning |
|---|---|---|
| `UNKNOWN_TEMPLATE_KIND`, `INVALID_DATE`, `EMPTY_SOURCE` | 400 | a malformed request |
| `SUBJECT_REF_REQUIRED`, `NO_CHANNEL`, `UNSCOPED_QUERY` | 400 | an issue or a query missing the key that scopes it |
| `FORBIDDEN` | 403 | the Cedar policy denied the action |
| `TEMPLATE_NOT_FOUND`, `NO_CURRENT_TEMPLATE` | 404 | nothing of this tenant answers to that |
| `DOCUMENT_NOT_FOUND`, `PORTAL_DELIVERY_NOT_FOUND` | 404 | no such issued document / portal delivery in this tenant |
| `TEMPLATE_IDENTITY_TAKEN` | 409 | you have already published this exact source under another kind |
| `TEMPLATE_DID_NOT_COMPILE`, `TEMPLATE_REJECTED_BY_GATE` | 422 | with `diagnostics` |
| `TEMPLATE_NOT_PUBLISHED` | 422 | the rollout names a hash never published as this kind |
| `TEMPLATE_WRONG_KIND`, `SUBJECT_REQUIRED`, `SUBJECT_MUST_BE_A_MODEL`, `SUBJECT_MUST_BE_A_VIEW`, `VIEW_NOT_AN_OBJECT` | 422 | the render's parts do not agree |
| `ANREDE_IN_VIEW`, `ANREDE_UNKNOWN`, `ANREDE_FIXED` | 422 | the form of address came from the view, names no option, or asks `du` of a Gebührenbescheid |
| `PAPIER_UNZULAESSIG` | 422 | a `POST`-only invoice the UStG does not admit on paper, or a missing fact |
| `CROSS_BORDER_SUPPLY_UNSUPPORTED`, `EXPORT_SUPPLY_UNSUPPORTED`, `RECIPIENT_COUNTRY_MISSING`, `RECIPIENT_COUNTRY_INVALID` | 422 | an `INVOICE` whose buyer is not in Germany, or whose BT-55 is absent or malformed |
| `ATTACHMENT_REQUIRED`, `ATTACHMENT_NOT_ALLOWED`, `ATTACHMENT_UNUSABLE`, `ATTACHMENT_NAME_INVALID` | 422 | the carrier contract |
| `PDF_STANDARD_UNUSABLE` | 422 | a level that would silently drop the invoice |
| `DATE_NOT_REPRESENTABLE` | 422 | a document date the PDF date format cannot carry |
| `NO_SPECIMEN` | 422 | this kind has no preview specimen — publish it instead |
| `RENDER_BUDGET_EXCEEDED` | 422 | the template is doing far more work than one document needs |
| `INTERNAL` | 500 | an unexpected failure; the cause is logged, never returned — the body carries an `error_id` the log line carries |

## Templates

The **layout** belongs to the operator — a [Typst](https://typst.app) template
published over the API and pinned by hash. The **content** belongs to the
issuing service, which validates the payload against its declared profile
before it reaches outputd.

```mermaid
graph LR
    caller[("caller<br/>(billingd)")]
    view["view JSON<br/>(the template contract)"]
    cii["CII XML + BT-24<br/>(rendered & proven by the caller)"]
    tpl[/"operator template<br/>let render(invoice)"/]
    harness["mako harness /main.typ<br/>import · pdf.attach · render"]
    typ["Typst → PDF/A-3"]
    xmp["facturx::stamp<br/>Factur-X XMP"]
    out["ZUGFeRD PDF + X-Mako-Template-Hash"]

    caller --> view --> harness
    caller --> cii --> harness
    tpl --> harness --> typ --> xmp --> out
```

### The template contract

A template exports exactly one function:

```typst
#let render(invoice) = { .. }
```

`invoice` is `document::view::DocumentView` as a Typst dictionary, projected by
outputd from the EN 16931 model: the §14 Abs. 4 UStG Pflichtangaben, lines, VAT
breakdown and totals, each field documented with its BT/BG term. Amounts are
exact decimal strings at their term's scale (money 2, unit price 4); a template
pads and never truncates. `nachrechnen` is `none` unless the issuer sent a
link — read it with `invoice.at("nachrechnen", default: none)` and place the
QR code with `image(bytes(n.qr_svg), format: "svg")`; the gate's specimen
carries one. `GET /api/v1/templates/reference/{kind}` serves mako's
reference layout (DIN 5008, German number formatting), compiled by the test
suite against the gate's specimen on every run.

### What a template cannot do

mako compiles **its own** entry file, not the operator's. The harness imports
the template, hands it the view, and emits the `pdf.attach` itself:

```typst
#import "/template.typ": render
#pdf.attach("factur-x.xml", bytes("<?xml version=\"1.0\" .."), relationship: "alternative", ..)
#render(json("/document.json"))
```

So a template cannot omit, rename, replace or read the invoice: the XML is a
literal in mako's file, not a file the compiler serves. The environment:

| Capability | Available to a template |
|---|---|
| Host filesystem | none — three in-memory files exist and nothing else |
| Network, `@preview` packages | refused, with a message explaining why |
| Fonts | the bundled Typst set; never the host's, never the operator's |
| Wall clock | none — `datetime.today()` returns the *document's* date (BT-2) |

Compute is not sandboxed: a render runs on a blocking thread under a 20 s budget
(`RENDER_BUDGET_EXCEEDED`), and concurrent renders are capped at one fewer than
the machine's cores so typesetting cannot starve `sqlx` on the shared blocking
pool. Waiting for a slot counts against the budget; a timed-out render keeps its
slot until it actually ends.

### The Factur-X carrier

A PDF/A-3 with the XML stapled inside it is not yet a ZUGFeRD document.
ZUGFeRD 2.3 requires four things of the carrier:

| Requirement | Written by |
|---|---|
| PDF/A-3 conformance | `typst-pdf` (enforced, not claimed) |
| `factur-x.xml` — or `xrechnung.xml` for the XRECHNUNG profile | the harness |
| `/AFRelationship /Alternative` + catalogue `/AF` | Typst |
| XMP `fx:DocumentType` / `DocumentFileName` / `Version` / `ConformanceLevel`, **plus** the PDF/A extension schema description | `document::facturx::stamp` |

The XMP is added by **incremental update** — the renderer's bytes stay in place
and a new metadata object is appended, the mechanism a digital signature uses.
The profile is derived from BT-24, never configured:
plain EN 16931 gets `factur-x.xml` and level `EN 16931`; the XRechnung CIUS gets
`xrechnung.xml` and `XRECHNUNG`.

### Publishing is gated by proof

`POST /api/v1/templates` renders the candidate against an awkward specimen —
two VAT rates, an exempt position with a BT-120 reason, a credit line, a
four-decimal unit price, umlauts, a long item name, absent optional fields —
then:

1. enforces the declared PDF/A level (one that cannot embed a file is refused);
2. stamps the Factur-X XMP;
3. reads the document back with **the counterparty's reader**
   (`en16931-formats::zugferd::extract`, then `en16931_formats::read`): the
   payload must be byte-identical, re-parse as CII with **no reader entry**,
   carry the same BT-1 and BT-115, and produce **no `Divergence`** (XMP profile ≠ BT-24, XMP filename ≠ attachment,
   supplementary `/AFRelationship`, missing XMP);
4. reads the **page** text and requires the invoice number (§ 14 Abs. 4 Nr. 4
   UStG), both party names (Nr. 1) and the USt-IdNr. (BT-31) *or* Steuernummer
   (BT-32) (Nr. 2) — so `#let render(invoice) = []` fails;
5. refuses more than 8 pages on the specimen (3 for every Textform kind).

The specimen is a real `en16931::Invoice`, rendered through
`DocumentView::of` and `facturx::cii_payload` (`en16931_formats::write_unchecked`
to CII) like production; a two-sided test
pins its BT-23, BT-34, BG-14 and BG-16 to what billingd's `einvoice::build`
stamps. A failing template answers `422` with diagnostics.
`document_templates.proof` records `RENDERED_PDFA` or `RENDERED_TEXTFORM`, and a
`CHECK` ties each kind to its proof.

`POST /api/v1/templates/preview` runs the same render for every kind but
`PREISANPASSUNG` and stores nothing; `PREISANPASSUNG` answers `422 NO_SPECIMEN`
and is checked by publishing.

### Renders are reproducible

Nothing ambient reaches the output: the date is BT-2, the PDF `/ID` derives
from tenant, template hash and record id, and the fonts are compiled in — so a
re-render produces the same bytes
(`rendering_the_same_invoice_twice_produces_the_same_file`). The issuing service
pins `X-Mako-Template-Hash` beside its record (billingd:
`billing_records.template_hash`); a draft pins nothing.

### The template store

Templates live in `document_templates`, **content-addressed and append-only**,
identified by `(tenant, SHA-256 of the source)` — scoped to the tenant, so two
tenants publishing the reference layout unchanged get two rows and never see
each other. The pin lives in another service's database, so append-only is the
whole guarantee that it resolves for the 8 years an invoice is kept (§ 14b UStG
/ § 147 AO, GoBD *Unveränderbarkeit*). `document_template_current` is the
mutable pointer per `(tenant, kind)`.

| Kind | Output | Default conformance | Proof |
|---|---|---|---|
| `INVOICE` | ZUGFeRD PDF/A-3 carrier | `a-3b` | `RENDERED_PDFA` |
| `MAHNUNG` | Textform (§ 126b BGB) | — | `RENDERED_TEXTFORM` |
| `PREISANPASSUNG` | § 41 Abs. 5 EnWG notice, Textform | — | `RENDERED_TEXTFORM` |
| `GEBUEHRENBESCHEID` | Bescheid over a public-law Gebühr | — | `RENDERED_TEXTFORM` |
| `VERTRAGSZUSAMMENFASSUNG` | § 41 Abs. 4 EnWG summary, Textform | — | `RENDERED_TEXTFORM` |
| `VERTRAGSBESTAETIGUNG` | § 312f Abs. 2 BGB confirmation, Textform | — | `RENDERED_TEXTFORM` |
| `WIDERRUFSBESTAETIGUNG` | § 356a Abs. 4 BGB Eingangsbestätigung, Textform | — | `RENDERED_TEXTFORM` |

| Document | Source | The gate requires |
|---|---|---|
| Mahnung (Stufe-3 specimen) | § 126b BGB (Textform) | the § 126b declarant, the Gesamtforderung, the Zahlungsfrist and the § 41f Sperrtermin |
| Preisanpassung (one price up, one down) | § 41 Abs. 5 Satz 1 and 4 EnWG: the change, its Anlass and Umfang and the Sonderkündigungsrecht in the same notice | the declarant, the Wirksamkeit, both changed prices and the Kündigungsrecht date |
| Gebührenbescheid (two Gebühren) | § 157 Abs. 1 AO: the levy „nach Art und Betrag", „wer die Steuer schuldet", and a Belehrung „welcher Rechtsbehelf zulässig ist und binnen welcher Frist und bei welcher Behörde er einzulegen ist" | the issuing body, the Schuldner, each Gebühr's Art and Betrag, the sum, and the Rechtsbehelf, Frist and Behörde |
| Vertragszusammenfassung | § 41 Abs. 4 Satz 2 EnWG: „1. die Kontaktdaten des Energielieferanten, 2. die Verbrauchsstelle, 3. geltende Preise, 4. den voraussichtlichen Belieferungsbeginn, 5. die Kündigungsfrist sowie 6. etwaige Bonusvereinbarungen und Mindestvertragslaufzeiten" | the supplier's name and one of its telephone number, e-mail or address, the Verbrauchsstelle, both specimen prices, the Belieferungsbeginn, the Kündigungsfrist, the Bonus and the Mindestvertragslaufzeit — the specimen carries Nr. 6, so a template that drops it is refused |
| Vertragsbestätigung | Art. 246a § 1 Abs. 2 Satz 1 Nr. 1 and 3 EGBGB | the declarant, the Vertragsnummer, the date of the Vertragsschluss, the product, both prices, the Belieferungsbeginn, the Laufzeit, the Kündigungsfrist and the Widerrufsbelehrung — its Frist, its text, the Muster-Widerrufsformular and the Wertersatz notice |
| Widerrufsbestätigung | § 356a Abs. 4 BGB | the declarant, the Vertragsnummer, the date **and** the time of receipt, and the declaration's name, contract and address for the confirmation |

Each mandated item has its own refused-template test in `document::gate`, and
the views (`document::mahnung::MahnungView` projects accountingd's Mahnwesen:
Stufe 1–3, open Posten, § 288 BGB Verzugszinsen, Mahngebühr, Sperrandrohung)
live with the renderer, which `accountingd`, `vertragd` and `billingd` conform
to.

Publishing returns the hash, proof, page count and any Typst warnings;
identical source stores nothing new. Publishing and rolling out are separate
decisions: `GET /api/v1/templates` lists newest first with `is_current`, and a
rollback is the same `PUT` with the previous hash. There is **no update and no
delete** endpoint. The listing omits the source; `by-hash` returns it.

### Verifying a generated document

`just zugferd-specimen` writes stamped specimens to `target/`; `just zugferd-verify` runs the external panel in containers (veraPDF, Mustang under Temurin):

| Check | Tool | Status |
|---|---|---|
| Payload against the 227 EN 16931 rules | `en16931 validate` (`cargo install en16931-cli`) | **valid — 0 findings**, an independent implementation reading what mako wrote |
| Payload validity *before* embedding | the caller (billingd), and the publish gate for its specimen | enforced |
| Carrier round-trip, `Divergence`, page content | mako's publish gate | enforced on every publish |
| XMP well-formed, every `fx:` property declared in its extension schema | `tests/it/zugferd_carrier.rs` | enforced |
| The incremental update disturbs **exactly one object**, `/ID` `/Root` `/Size` preserved | `tests/it/zugferd_carrier.rs` | enforced |
| **PDF/A-3b conformance** | veraPDF (`verapdf/cli:latest`) | **compliant, 0 failed rules** — both profiles and the pre-stamp control |
| XRechnung-profile payload (core + BR-DE) | `en16931 validate` | **valid — 0 findings of 282 rules** |
| Carrier + payload against the ZUGFeRD specification | Mustang 2.25.0 (reference validator) | **valid, both profiles** — XRechnung profile with zero findings; core profile carries one upstream warning (below) |

veraPDF and Mustang are not part of `just ci`; re-run `just zugferd-verify`
after any change to `document::facturx`.

Mustang raises `PEPPOL-EN16931-R008` on the core-profile file for the empty
`<ram:ApplicableHeaderTradeDelivery/>`. It is a false positive: the element is
mandatory in the CII D16B XSD and KoSIT's Schematron carves it out of R008. Do
not omit the element. `en16931-formats` writes it empty whenever the model
carries no BG-13 delivery term, which the specimen does not;
`zugferd_carrier::the_cii_payload_keeps_the_mandatory_empty_delivery_element`
pins it.

## Configuration

```toml
# outputd.toml
tenant = "stadtwerke-musterstadt"   # data-isolation key — every template row is scoped to it

[http]
addr = "0.0.0.0:9880"

[database]
url = "postgresql://outputd:secret@db:5432/outputd"

# Required unless allow_insecure_no_auth = true (dev only).
[oidc]
issuer   = "https://idp.example/realms/mako"
audience = "outputd"

# Every outbound channel is an HTTP relay. A relay URL requires its key — the
# bearer token and the Standard Webhooks signing key; a URL without one is a
# startup refusal.
[delivery]
enabled             = true          # default
from_address        = "rechnung@stadtwerke-musterstadt.example"
email_relay_url     = "https://mail-relay.internal/api/v1/send"
email_relay_api_key = "env:OUTPUTD_MAIL_RELAY_KEY"
# Optional: unset selects the pull model (GET /api/v1/spool).
postal_relay_url    = "https://druckdienstleister.example/api/v1/jobs"
postal_relay_api_key = "env:OUTPUTD_PRINT_KEY"
# The ERP channel — the operator's own system, which then owns delivery.
erp_webhook_url     = "https://erp.internal/api/v1/documents"
erp_api_key         = "env:OUTPUTD_ERP_KEY"

# Subject lines per kind, used verbatim; a kind with no entry falls back to a
# built-in in the document's anrede („Ihre Rechnung“ / „Deine Rechnung“).
[delivery.subjects]
INVOICE        = "Ihre Rechnung"
MAHNUNG        = "Zahlungserinnerung"
PREISANPASSUNG = "Änderung Ihrer Preise"
GEBUEHRENBESCHEID = "Gebührenbescheid"
VERTRAGSZUSAMMENFASSUNG = "Zusammenfassung Ihres Vertrags"
VERTRAGSBESTAETIGUNG    = "Bestätigung Ihres Vertrags"
WIDERRUFSBESTAETIGUNG   = "Eingangsbestätigung Ihres Widerrufs"
```

### `[policy] anrede` {#policy-anrede}

```toml
[policy]
anrede = "du"   # default "sie"
```

How a customer letter addresses its reader. § 126b BGB asks a Textform letter
for a readable declaration that names the declarant and fixes no form of
address, so the voice is the operator's.

| `anrede` | Effect |
|---|---|
| `sie` (default) | „Sie“, „Ihr“, capitalised; „Sehr geehrte Damen und Herren“, „Mit freundlichen Grüßen“ |
| `du` | „du“, „dein“, lower-case in a letter as current spelling writes it; „Hallo“, „Viele Grüße“ |

It covers the reference layouts of the Mahnung, Preisanpassung,
Vertragsbestätigung, Vertragszusammenfassung and Widerrufsbestätigung, and the
built-in mail subjects (`[delivery.subjects]` entries are the operator's own
words and go out verbatim). outputd writes no mail body; a relay receives the
subject and the document. Two texts do not follow it and stay „Sie“:

- the **Widerrufsbelehrung** with its Muster-Widerrufsformular inside the
  Vertragsbestätigung — the statutory Muster (Art. 246a § 1 Abs. 2 EGBGB) keeps
  its effect only as published, so the template prints it as vertragd sends it;
- the **Gebührenbescheid** — a Verwaltungsakt of a municipal operator, not a
  supplier's letter. Its view gets no `anrede` and it records `sie`.

A template reads the setting as `anrede` on its view; the reference layouts pick
each phrase with `form[Sie text][du text]`. A free-text field a producer sends
— the Mahnung's `sperrandrohung`, the Preisanpassung's `anlass` and `hinweis` —
is printed verbatim, so producers phrase it without a pronoun of address. The
publish gate proves a template against its specimen in `sie`; a preview renders
in the deployment's setting. Each issued document records its `anrede`
(`GET /api/v1/documents/{id}`), and the setting is reported at
`GET /api/v1/policy` with its source.

Callers configure the counterpart: `outputd_url` plus an optional
`outputd_api_key` bearer in billingd, accountingd, vertragd and portald. Without
a reachable outputd, billingd's PDF endpoint answers `502` (the XML endpoints
need no renderer), and accountingd and vertragd record their dunning and
price-change facts without producing a document — visibly, in the log.
