> For the complete documentation index, see [llms.txt](https://docs.hubyte.de/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.hubyte.de/artikel-konfigurator/english-documentation/ausgabe-bestellung-belege/pdf-dokumente.md).

# PDF documents & variables

The filled-in fields can also be output on **PDF documents** (invoice, delivery note, credit note, cancellation invoice). The plugin already includes an extension of the standard document template – but you can also customize the output in your own template.

## Enabling the default output

For a field to appear on documents, the **Show in order documents** option must be enabled in the field settings (see [Creating fields & field types](/artikel-konfigurator/english-documentation/felder-anlegen.md)). The plugin then outputs the attributes automatically per order line item:

* **Without a price** – Name of the attribute and the selected value (for color selection the color name).
* **With a price** – Name, selected option, quantity, tax rate, unit and total price of the surcharge.

The presentation follows Shopware's document options (e.g. `config.displayPrices`, `config.displayLineItemPosition`).

## Available variables per order line item

On documents the line item variable is called `lineItem`. The fields are stored in the payload – just like in the email:

| Variable                                  | Content                                                                                                                      |
| ----------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------- |
| `lineItem.payload.huebert_attributes`     | All filled-in fields **without** a separate price.                                                                           |
| `lineItem.payload.attributes_with_prices` | Fields/options **with a surcharge** (only when "prices" are enabled).                                                        |
| `lineItem.payload.dependencies`           | Fields confirmed via a [dependency (checkbox)](/artikel-konfigurator/english-documentation/abhaengigkeiten.md), incl. price. |

In addition, these standard variables are useful in the document template:

| Variable                                     | Meaning                                        |
| -------------------------------------------- | ---------------------------------------------- |
| `lineItem.quantity`                          | Quantity of the line item.                     |
| `lineItem.unitPrice` / `lineItem.totalPrice` | Unit and total price of the line item.         |
| `lineItem.price.taxRules.first.taxRate`      | Tax rate of the line item.                     |
| `currencyIsoCode`                            | Currency ISO code (for the `currency` filter). |
| `config.displayPrices`                       | Document option: show prices.                  |
| `config.displayLineItemPosition`             | Document option: show position number.         |

The exact structure of `huebert_attributes` and `attributes_with_prices` is identical to the email – see [Email template & variables](/artikel-konfigurator/english-documentation/ausgabe-bestellung-belege/email-template.md).

## Customizing the document template

If you want to design the presentation yourself, override the `position` block in the document template. You have two options:

1. **Own theme/plugin** – Create a file `views/documents/base.html.twig` that extends `@Framework/documents/base.html.twig`.
2. **Plugin "Edit document template" (`HuebertCustomDocuments`)** – Lets you adapt the templates directly in the administration without creating your own files.

### Template: extend the `position` block

```twig
{% sw_extends '@Framework/documents/base.html.twig' %}

{% block position %}
    {{ parent() }}

    {% if config.displayLineItems %}
        {# Fields without a price #}
        {% if lineItem.payload.huebert_attributes is defined and lineItem.payload.huebert_attributes %}
            {% for attributesList in lineItem.payload.huebert_attributes %}
                {% for key, attribute in attributesList|sort((a, b) => a.order <=> b.order) %}
                    {% if key != 'quantity' and attribute.name is defined %}
                        <tr class="line-item">
                            {% if config.displayLineItemPosition %}<td></td>{% endif %}
                            <td>{{ attribute.name|replace({'_': ' '}) }}</td>
                            <td>
                                {% if attribute.value.colorName is defined %}
                                    {{ attribute.value.colorName }}
                                {% else %}
                                    {{ attribute.value }}
                                {% endif %}
                            </td>
                            <td class="align-right"></td>
                            {% if config.displayPrices %}
                                <td class="align-right"></td>
                                <td class="align-right"></td>
                                <td class="align-right"></td>
                            {% endif %}
                        </tr>
                    {% endif %}
                {% endfor %}
            {% endfor %}
        {% endif %}

        {# Options with a surcharge #}
        {% if lineItem.payload.attributes_with_prices is defined and lineItem.payload.attributes_with_prices %}
            {% for optionName, options in lineItem.payload.attributes_with_prices %}
                {% for name, array in options %}
                    {% set price = array.price|number_format(2)|floatval * array.quantity %}
                    <tr class="line-item">
                        {% if config.displayLineItemPosition %}<td></td>{% endif %}
                        <td>{{ optionName }}</td>
                        <td>{{ name }}</td>
                        <td class="align-right">{{ array.quantity }}</td>
                        {% if config.displayPrices %}
                            <td class="align-right">{{ lineItem.price.taxRules.first.taxRate }}%</td>
                            <td class="align-right">{{ array.price|number_format(2)|floatval|currency(currencyIsoCode) }}</td>
                            <td class="align-right">{{ price|currency(currencyIsoCode) }}</td>
                        {% endif %}
                    </tr>
                {% endfor %}
            {% endfor %}
        {% endif %}
    {% endif %}
{% endblock %}
```

## Examples

### Output only a specific field on the delivery note

```twig
{% block position %}
    {{ parent() }}
    {% if lineItem.payload.huebert_attributes is defined %}
        {% for attributesList in lineItem.payload.huebert_attributes %}
            {% for key, attribute in attributesList %}
                {% if attribute.name == 'Engraving' %}
                    <tr class="line-item">
                        <td colspan="2"><strong>Engraving:</strong> {{ attribute.value }}</td>
                    </tr>
                {% endif %}
            {% endfor %}
        {% endfor %}
    {% endif %}
{% endblock %}
```

### Subtract surcharges from the line item price

If the document should show the **plain product price** without the surcharges, you can calculate the sum of the attribute prices and subtract it from the line item price (analogous to the shipped template):

```twig
{% set totalAttributes = 0 %}
{% if lineItem.payload.attributes_with_prices is defined %}
    {% for optionName, options in lineItem.payload.attributes_with_prices %}
        {% for name, array in options %}
            {% set totalAttributes = totalAttributes + (array.price|number_format(2)|floatval * array.quantity) %}
        {% endfor %}
    {% endfor %}
{% endif %}
{% set productUnitPrice = lineItem.unitPrice - totalAttributes %}
```

> **Tip:** After every template change, generate a test document for a real order to check the presentation. Make sure `currencyIsoCode` is set before using the `currency` filter – in the standard document template the variable is already available.
