# Layouts

Every layout below is drawn by one call, `SefazDanfe.render/2`. The previews are the first page of
the PDF rendered at 100 dpi from the synthetic XML of the test suite; regenerate them with
`mix run scripts/previews.exs`. The text printed on the documents stays in Portuguese, as the
official manuals (MOC, DACTE and DAMDFE) require.

The document is recognised from the XML, so the only thing that changes between the calls is the
options.

## DANFE, portrait

The default layout of an NF-e (model 55) whose `tpImp` is 1: canhoto, emitter, access key with its
CODE-128C barcode, authorization protocol, recipient, invoice and installments, tax calculation,
carrier and volumes, items, ISSQN and the additional data at the foot of the last sheet. A
document with many items runs over several sheets, repeating the header and the column titles and
numbering them "Folha N/M".

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

![DANFE, portrait](images/danfe-portrait.png)

## DANFE, landscape

A4 on its side, with the vertical canhoto on the left, as the MOC (Anexo II) lays it out. It is
chosen by `tpImp` 2 in the XML, or forced with `orientation: :landscape` (`:portrait` forces the
other way; `:auto` is the default).

```elixir
{:ok, pdf} = SefazDanfe.render(File.read!("nfe-proc.xml"), orientation: :landscape)
```

![DANFE, landscape](images/danfe-landscape.png)

## DANFE NFC-e, 80 mm

The receipt of an NFC-e (model 65), with the QR code 2.00. The paper is 80 mm wide by default; the
page is as tall as the content.

```elixir
{:ok, pdf} = SefazDanfe.render(File.read!("nfce-proc.xml"))
```

![DANFE NFC-e, 80 mm](images/danfce-80mm.png)

## DANFE NFC-e, 58 mm

The same document on the narrow roll.

```elixir
{:ok, pdf} = SefazDanfe.render(File.read!("nfce-proc.xml"), paper: :mm58)
```

![DANFE NFC-e, 58 mm](images/danfce-58mm.png)

`copy: :establishment` prints "Via do Estabelecimento" next to the emission date and time.

## DACTE

The auxiliary document of a CT-e (model 57), laid out after the DACTE of the MOC for the road
modal (RNTRC and the road data the XML carries).

```elixir
{:ok, pdf} = SefazDanfe.render(File.read!("cte-proc.xml"))
```

![DACTE](images/dacte.png)

## DACTE OS

The CT-e OS (model 67, `cteOSProc`) uses the same call.

```elixir
{:ok, pdf} = SefazDanfe.render(File.read!("cte-os-proc.xml"))
```

![DACTE OS](images/dacte-os.png)

## DAMDFE

The auxiliary document of an MDF-e, with its QR code, the vehicle and the linked documents.
`closed: true` marks a closed (encerrado) MDF-e.

```elixir
{:ok, pdf} = SefazDanfe.render(File.read!("mdfe-proc.xml"))
{:ok, closed} = SefazDanfe.render(File.read!("mdfe-proc.xml"), closed: true)
```

![DAMDFE](images/damdfe.png)

## Marks and options on every layout

  * `canceled: true` (NF-e, NFC-e) marks the document as canceled ("NF-e CANCELADA" banner and a "CANCELADA" watermark).
  * An XML of the homologation environment (`tpAmb` 2) prints "SEM VALOR FISCAL"; no option is
    needed.
  * `logo: {:png, binary}` or `logo: {:svg, binary}` draws the emitter's logo in a fixed box.

```elixir
SefazDanfe.render(xml, canceled: true, logo: {:png, File.read!("logo.png")})
```

## Contingency examples

The notice comes from the `tpEmis` of the XML; nothing is asked of the caller. The texts and the
sources are in `SefazDanfe.Contingency`.

### NF-e, FS-DA

`tpEmis` 5 prints "DANFE EM CONTINGÊNCIA - IMPRESSO EM DECORRÊNCIA DE PROBLEMAS TÉCNICOS", the
second barcode "DADOS DA NF-E" (Code 128C of 36 digits, in place of the consulta URL) and the same
digits in the protocol box. The entry time (`dhCont`) and the reason (`xJust`) go in the additional
data.

```elixir
{:ok, pdf} = SefazDanfe.render(File.read!("nfe-fsda.xml"))
```

![DANFE in FS-DA contingency](images/danfe-fsda.png)

### NFC-e, offline

`tpEmis` 9 prints "EMITIDA EM CONTINGÊNCIA" and "Pendente de autorização", with no protocol line.
The QR code still prints.

```elixir
{:ok, pdf} = SefazDanfe.render(File.read!("nfce-offline.xml"))
```

![DANFE NFC-e offline](images/danfce-offline.png)

### MDF-e, contingency

`tpEmis` 2 prints "EMISSÃO EM CONTINGÊNCIA" in the place of the authorization protocol.

```elixir
{:ok, pdf} = SefazDanfe.render(File.read!("mdfe-contingency.xml"))
```

![DAMDFE in contingency](images/damdfe-contingency.png)

The DACTE has its own FS-DA notice ("DACTE EM CONTINGÊNCIA - IMPRESSO EM DECORRÊNCIA DE PROBLEMAS
TÉCNICOS", with the second barcode "DADOS DO CT-E"); the EPEC emission of an NF-e prints "DANFE
impresso em contingência - EPEC regularmente recebida pela Receita Federal do Brasil".
