# `SefazDanfe.Renderer`
[🔗](https://github.com/devaction-labs/sefaz_danfe/blob/v0.1.0/lib/sefaz_danfe/renderer.ex#L1)

Draws the PDF of a document from the data `SefazDanfe.data/2` reads: the one place that calls
Typst.

The layouts are `priv/templates/{danfe,danfe_landscape,danfce,dacte,damdfe}.typ`, compiled by Typst (the `typst`
package, a precompiled NIF: no external binary and no browser). A template is static: the data
goes in as a JSON file of Typst's virtual file system, never interpolated into the markup, so
nothing in an XML can be read as Typst. The fonts are the ones in `priv/fonts` (IBM Plex Sans, SIL
OFL), loaded from disk through `Application.app_dir/2`: no font and no package is fetched at run
time.

The bars of the access key come from `SefazDanfe.Barcode` and the QR code from
`SefazDanfe.QrCode`; the templates draw both as rectangles.

# `type`

```elixir
@type type() :: :nfe | :nfce | :cte | :mdfe
```

# `check_logo`

```elixir
@spec check_logo(term()) :: :ok | {:error, {:logo, term()}}
```

Checks a `:logo` option: `{:png, binary}` or `{:svg, binary}` whose bytes are of that format
(the PNG signature; an `<svg` element). `:ok` or `{:error, {:logo, reason}}`.

# `render`

```elixir
@spec render(type(), map(), keyword()) :: {:ok, binary()} | {:error, term()}
```

The PDF of `data` (the map `SefazDanfe.data/2` answers for a document of `type`).

`opts` are the options that shape the drawing, already validated by `SefazDanfe`: `:logo`
(`{:png | :svg, binary}`, a virtual file of the template), `:paper` (`:mm80` or `:mm58`, the
NFC-e receipt), `:copy` (`:consumer` or `:establishment`, the copy the NFC-e receipt is) and `:orientation` (`:portrait`, the default, or `:landscape`, which only applies
to the DANFE of the NF-e: `:auto` is resolved by `SefazDanfe` from the `tpImp` and anything but
`:landscape` draws the portrait).

Errors: `{:error, :no_access_key}` for data without the key, `{:error, {:barcode, reason}}` and
`{:error, {:qr_code, reason}}` for a key or QR text that cannot be encoded, and
`{:error, {:typst, reason}}` when Typst cannot compile the layout with that data.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
