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

The printed auxiliary documents of the Brazilian electronic fiscal documents, drawn from the
authorized XML: the DANFE of an NF-e (model 55), the DANFE NFC-e (model 65), the DACTE of a CT-e
(57) or CT-e OS (67) and the DAMDFE of an MDF-e (58).

The XML is read with the parser of `sefaz_nfe`. The document is recognised from the XML itself:

| XML                                        | Document        |
| ------------------------------------------ | --------------- |
| `nfeProc` / `NFe` with `mod` 55            | DANFE           |
| `nfeProc` / `NFe` with `mod` 65            | DANFE NFC-e     |
| `cteProc` / `CTe` (57), `cteOSProc` / `CTeOS` (67) | DACTE   |
| `mdfeProc` / `MDFe`                        | DAMDFE          |

    {:ok, pdf} = SefazDanfe.render(File.read!("nfe-proc.xml"))
    File.write!("danfe.pdf", pdf)

    {:ok, data} = SefazDanfe.data(File.read!("nfe-proc.xml"))
    data["number"]
    #=> "000.001.001"

## Options

  * `:canceled` - the NF-e or NFC-e is canceled (event 110111): the PDF says so. Default `false`.
  * `:closed` - the MDF-e is closed (encerrado): the PDF says so. Default `false`.
  * `:logo` - the emitter's logo, `{:png, binary}` or `{:svg, binary}` (the format is named, not
    guessed; the bytes are checked against it). Drawn in the emitter block of the DANFE, DANFE
    NFC-e, DACTE and DAMDFE inside a fixed box, scaled to fit without distortion. Default `nil`
    (no logo).
  * `:paper` - the width of the DANFE NFC-e receipt: `:mm80` (default) or `:mm58`.
  * `:copy` - the copy of the DANFE NFC-e: `:consumer` (default) or `:establishment`, which prints
    "Via do Estabelecimento" next to the emission date and time.
  * `:orientation` - the page of the DANFE of an NF-e (model 55): `:portrait`, `:landscape` or
    `:auto` (the default), which follows the `tpImp` of the XML (2 is landscape, anything else
    is portrait). The landscape DANFE has the canhoto on the left, vertical, as the MOC
    (Anexo II) lays it out. Ignored for the other documents.

An option that does not apply to the document is accepted and ignored.

## Errors

  * `{:error, :unsupported_document}` - the XML is not one of the documents above;
  * `{:error, {:invalid_options, reason}}` - an unknown option or a value the option does not accept
    (`{:invalid_options, {:logo, reason}}`, `{:invalid_options, {:paper, value}}`, `{:invalid_options, {:copy, value}}`,
    `{:invalid_options, {:orientation, value}}`, `{:invalid_options, {:not_a_boolean, key}}`);
  * `{:error, {:unreadable, reason}}` - the XML cannot be parsed;
  * `{:error, :no_access_key}`, `{:error, {:barcode, reason}}`, `{:error, {:qr_code, reason}}` -
    the document has no usable access key or QR text;
  * `{:error, {:typst, reason}}` - Typst could not compile the layout.

## Telemetry

`render/2` is a `:telemetry.span/3` on `[:sefaz_danfe, :render]`: the `:start` and `:stop` events
(and `:exception`) carry `%{type: :nfe | :nfce | :cte | :mdfe}`. The `:stop` metadata also has
`result: :ok | :error`. A document that is not supported emits nothing.

# `reason`

```elixir
@type reason() :: :unsupported_document | {:unreadable, term()} | term()
```

# `type`

```elixir
@type type() :: SefazDanfe.Renderer.type()
```

# `data`

```elixir
@spec data(String.t(), keyword()) :: {:ok, map()} | {:error, reason()}
```

The data of the document in `xml`: a map with string keys, every value presented the Brazilian
way and `nil` when the XML does not have it; what the PDF is drawn from.

# `render`

```elixir
@spec render(String.t(), keyword()) :: {:ok, binary()} | {:error, reason()}
```

The PDF of the document in `xml`, or `{:error, reason}`. See the module documentation for the
options and the errors.

---

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