ESC-POS invoice printing
Invoices can be printed on a receipt (POS) printer as a narrow text receipt instead of an A4 PDF. To do this, you create an invoice template of type escpos. When an invoice is written with this template, Smarton saves it as a text file, and in POS you can send it straight to the printer.
What you need:
- a receipt printer that Smarton reaches through MQTT, meaning the printer device has its Mqtt topic filled in (step 3);
- an invoice template of type escpos (steps 1 and 2). Three ready-made templates are at the end of this page.
1. Create an ESC/POS template |
Dashboard -> Settings -> Templates -> +
In the dialog, select:
- Type: escpos
- Context: Invoice
and click CREATE.
Note: choosing the Type clears the Name field, so you can leave the name empty here and enter it on the next screen.
2. Enter the template code |
The template page opens. Enter a Name (for example "ESC/POS invoice") and paste the template code into the Escpos code box on the left. You can copy one of the templates at the end of this page. Then save the template with the save icon in the top right corner.
Important:
- Edit the code only in the Escpos code box. The Template body editor above shows the same code on one line; do not edit it there.
- The example on the right side of the page has comments after the commands (; Inicializuoti spausdintuvą). Do not copy the comments: a line like
ESC ! 0x08 ; Teksto padidinimasis ignored when printing. - The template language is taken from your user language when the template is created.
3. Set up the printer |
Dashboard -> Settings -> Integrations -> Devices -> edit the printer
Fill in the Mqtt topic field of the printer, for example devices/<device id>/printing, and click CONFIRM. The invoice is sent to the printer through this topic. If the field is empty, printing fails with the message "We can not print invoices with this printer".
Which printers POS offers depends on the setting Accounting -> Settings -> Company -> POS -> Use fiscal printer (if you do not see it, contact SmartOn support):
- when it is on, POS lists fiscal printers;
- when it is off, POS lists POS (receipt) printers.
Set the Mqtt topic on the printer of the type that POS uses.
4. Write the invoice in POS |
POS -> (your name) -> Organiser -> Payments
Find the order and click the document icon. In the Invoice template field, choose your ESC/POS template and click CREATE.
5. Print the invoice |
Orders that have an invoice now show a print icon.
Click it, choose the printer in the Select device dialog and click CONFIRM. If the order has several invoices, Smarton first asks which invoice to print. Invoices created with an ordinary (HTML) template are not printed; they open as a PDF preview.
Ready-made templates |
Copy the whole block into the Escpos code box (step 2). All templates are made for 80 mm paper (48 characters per line).
Template 1: Print test. Use it first to check that the printer prints, cuts the paper and shows Lithuanian letters correctly.
ESC @
ESC ! 0x30
TEXT "SPAUSDINIMO TESTAS\n"
ESC ! 0x00
TEXT "------------------------------------------------\n"
TEXT "Sąskaita: {{ system.invoiceAffix }}FF{{ '%04d'|format(invoice.number) }}\n"
TEXT "Data: {{ invoice.date|date('Y-m-d') }}\n"
TEXT "Suma: {{ invoice.sumWithVat.amount|number_format(2, '.', '') }} {{ invoice.sumWithVat.currency }}\n"
TEXT "Raidės: ąčęėįšųūž ĄČĘĖĮŠŲŪŽ\n"
TEXT "------------------------------------------------\n"
ESC d 4
GS V 1
Template 2: Short invoice. Company, invoice number and date, products and the total.
{% macro row(left, right) %}{% set n = 48 - left|length - right|length %}{{ left|raw }}{% if n > 0 %}{% for i in 1..n %} {% endfor %}{% else %} {% endif %}{{ right|raw }}{% endmacro %}
{% import _self as m %}
ESC @
ESC ! 0x08
TEXT "{{ system.organisationName|raw }}\n"
ESC ! 0x00
TEXT "{{ system.organisationAddress|striptags|raw }}\n"
TEXT "Įmonės kodas: {{ system.organisationCode }}\n"
TEXT "------------------------------------------------\n"
ESC ! 0x08
TEXT "Sąskaita-faktūra {{ system.invoiceAffix }}FF{{ '%04d'|format(invoice.number) }}\n"
ESC ! 0x00
TEXT "Data: {{ invoice.date|date('Y-m-d') }}\n"
TEXT "------------------------------------------------\n"
{% for record in records %}
TEXT "{{ record.name|raw }}\n"
TEXT "{{ m.row(' ' ~ record.quantity|number_format(0, '.', '') ~ ' x ' ~ record.price.amount|number_format(2, '.', ''), record.sum.amount|number_format(2, '.', '')) }}\n"
{% endfor %}
TEXT "------------------------------------------------\n"
ESC ! 0x08
TEXT "{{ m.row('Iš viso su PVM, ' ~ invoice.sumWithVat.currency, invoice.sumLocalCurrencyWithVat.amount|number_format(2, '.', '')) }}\n"
ESC ! 0x00
{% if invoice.checkNumber %}
TEXT "Kasos kvito Nr. {{ invoice.checkNumber }}\n"
{% endif %}
ESC d 4
GS V 1
Template 3: Full invoice. Based on the standard PDF invoice: invoice type (proforma, credit, debit), supplier with bank account, buyer, products with discount and VAT, VAT breakdown, payments, receipt number and the issuer.
{% macro row(left, right) %}{% set n = 48 - left|length - right|length %}{{ left|raw }}{% if n > 0 %}{% for i in 1..n %} {% endfor %}{% else %} {% endif %}{{ right|raw }}{% endmacro %}
{% import _self as m %}
{# To print one line instead of every product, write the line name here, e.g. 'Maitinimas' #}
{% set summaryName = '' %}
{% set cur = invoice.sumWithVat.currency %}
ESC @
ESC ! 0x08
{% if invoice.isProforma %}
TEXT "Išankstinė sąskaita {{ system.invoiceAffix }}{{ invoice.number }}\n"
{% elseif invoice.isCredit %}
TEXT "Kreditinė PVM sąskaita-faktūra\n"
TEXT "{{ system.invoiceAffix }}K Nr. {{ invoice.number }}\n"
{% elseif invoice.isDebit %}
TEXT "Debetinė PVM sąskaita-faktūra\n"
TEXT "{{ system.invoiceAffix }}D Nr. {{ invoice.number }}\n"
{% else %}
TEXT "Sąskaita-faktūra {{ system.invoiceAffix }}FF{{ '%04d'|format(invoice.number) }}\n"
{% endif %}
ESC ! 0x00
TEXT "Data: {{ invoice.date|date('Y-m-d') }}\n"
TEXT "================================================\n"
ESC ! 0x08
TEXT "Tiekėjas: {{ system.organisationName|raw }}\n"
ESC ! 0x00
TEXT "{{ system.organisationAddress|striptags|raw }}\n"
TEXT "Įmonės kodas: {{ system.organisationCode }}\n"
TEXT "PVM kodas: {{ system.organisationVatCode }}\n"
{% for bankAccount in system.bankAccounts %}
TEXT "Bankas {{ bankAccount.bankName|default('')|raw }}\n"
TEXT "Sąsk. nr. {{ bankAccount.accountNumber|default('') }}\n"
{% endfor %}
{% if invoice.customer %}
TEXT "------------------------------------------------\n"
ESC ! 0x08
TEXT "Pirkėjas: {{ invoice.customer.name|default('')|raw }}\n"
ESC ! 0x00
{% if invoice.customer.registrationCode %}
TEXT "Įmonės kodas: {{ invoice.customer.registrationCode }}\n"
{% endif %}
{% if invoice.customer.vatCode %}
TEXT "PVM kodas: {{ invoice.customer.vatCode }}\n"
{% endif %}
{% if invoice.customer.invoiceAddress %}
TEXT "Adresas: {{ invoice.customer.invoiceAddress|striptags|raw }}\n"
{% endif %}
{% endif %}
TEXT "================================================\n"
{% set vatGroups = {} %}
{% for record in records %}
{% set discountPct = record.orderProduct.discount|default(0) %}
{% set base = record.price.amount * record.quantity * (1 - discountPct / 100) %}
{% set pct = record.vat ? record.vat.percentage : 0 %}
{% set key = 'v' ~ pct %}
{% set prev = vatGroups[key]|default({'pct': pct, 'base': 0, 'vat': 0}) %}
{% set vatGroups = vatGroups|merge({(key): {'pct': pct, 'base': prev.base + base, 'vat': prev.vat + base * pct / 100}}) %}
{% if not summaryName %}
TEXT "{{ loop.index }}. {{ record.name|raw }}\n"
TEXT "{{ m.row(' ' ~ record.quantity|number_format(0, '.', '') ~ ' ' ~ record.unit ~ ' x ' ~ record.price.amount|number_format(2, '.', '') ~ ' PVM ' ~ pct ~ '%', record.sum.amount|number_format(2, '.', '')) }}\n"
{% if discountPct > 0 %}
TEXT "{{ m.row(' Nuolaida ' ~ discountPct ~ '%', '-' ~ (record.price.amount * record.quantity * discountPct / 100)|number_format(2, '.', '')) }}\n"
{% endif %}
{% endif %}
{% endfor %}
{% if summaryName %}
{% for group in vatGroups %}
TEXT "{{ m.row(summaryName ~ (vatGroups|length > 1 ? ' (PVM ' ~ group.pct ~ ' %)' : ''), group.base|number_format(2, '.', '')) }}\n"
{% endfor %}
{% endif %}
{% if invoice.includeTransportCosts %}
TEXT "{{ m.row(invoice.transportPriceText|default('Transportas'), invoice.transportPrice.amount|number_format(2, '.', '')) }}\n"
{% endif %}
TEXT "------------------------------------------------\n"
{% if not summaryName %}
TEXT "{{ m.row('Kiekis iš viso', invoice.quantity|number_format(0, '.', '')) }}\n"
{% endif %}
TEXT "{{ m.row('Suma be PVM, ' ~ cur, invoice.sum.amount|number_format(2, '.', '')) }}\n"
{% if invoice.discount.amount|default(0) > 0 %}
TEXT "{{ m.row('Nuolaida, ' ~ cur, invoice.discount.amount|number_format(2, '.', '')) }}\n"
{% endif %}
{% for group in vatGroups %}
TEXT "{{ m.row('Apmokestinama PVM ' ~ group.pct ~ ' %', group.base|number_format(2, '.', '')) }}\n"
{% if group.vat > 0 %}
TEXT "{{ m.row('Iš viso ' ~ group.pct ~ ' % PVM', group.vat|number_format(2, '.', '')) }}\n"
{% endif %}
{% endfor %}
ESC ! 0x08
TEXT "{{ m.row('Iš viso su PVM, ' ~ cur, invoice.sumLocalCurrencyWithVat.amount|number_format(2, '.', '')) }}\n"
ESC ! 0x00
{% if paymentTypes|length %}
TEXT "------------------------------------------------\n"
{% for payment in paymentTypes %}
TEXT "{{ m.row(payment.name|default(''), payment.sum.amount|number_format(2, '.', '')) }}\n"
{% endfor %}
{% endif %}
TEXT "------------------------------------------------\n"
{% if invoice.checkNumber %}
TEXT "Kasos kvito Nr. {{ invoice.checkNumber }}{% if order and order.finishedAt %}, išduotas {{ order.finishedAt|date('Y-m-d') }}{% endif %}\n"
{% endif %}
TEXT "Sąskaitą išrašė: {{ invoice.createdBy ? invoice.createdBy.name|raw : '' }}\n"
ESC d 2
TEXT "Priėmė ____________________________\n"
ESC d 5
GS V 1
Template 4: Invoice with one "Food service" line. The same as Template 3, but instead of listing every product it prints a single line Maitinimas (Lithuanian for "Food service") with the sum. If the products have different VAT rates, one line is printed per rate, for example Maitinimas (PVM 21 %) and Maitinimas (PVM 9 %). Totals, VAT and payments are the same as in Template 3.
{% macro row(left, right) %}{% set n = 48 - left|length - right|length %}{{ left|raw }}{% if n > 0 %}{% for i in 1..n %} {% endfor %}{% else %} {% endif %}{{ right|raw }}{% endmacro %}
{% import _self as m %}
{# To print one line instead of every product, write the line name here, e.g. 'Maitinimas' #}
{% set summaryName = 'Maitinimas' %}
{% set cur = invoice.sumWithVat.currency %}
ESC @
ESC ! 0x08
{% if invoice.isProforma %}
TEXT "Išankstinė sąskaita {{ system.invoiceAffix }}{{ invoice.number }}\n"
{% elseif invoice.isCredit %}
TEXT "Kreditinė PVM sąskaita-faktūra\n"
TEXT "{{ system.invoiceAffix }}K Nr. {{ invoice.number }}\n"
{% elseif invoice.isDebit %}
TEXT "Debetinė PVM sąskaita-faktūra\n"
TEXT "{{ system.invoiceAffix }}D Nr. {{ invoice.number }}\n"
{% else %}
TEXT "Sąskaita-faktūra {{ system.invoiceAffix }}FF{{ '%04d'|format(invoice.number) }}\n"
{% endif %}
ESC ! 0x00
TEXT "Data: {{ invoice.date|date('Y-m-d') }}\n"
TEXT "================================================\n"
ESC ! 0x08
TEXT "Tiekėjas: {{ system.organisationName|raw }}\n"
ESC ! 0x00
TEXT "{{ system.organisationAddress|striptags|raw }}\n"
TEXT "Įmonės kodas: {{ system.organisationCode }}\n"
TEXT "PVM kodas: {{ system.organisationVatCode }}\n"
{% for bankAccount in system.bankAccounts %}
TEXT "Bankas {{ bankAccount.bankName|default('')|raw }}\n"
TEXT "Sąsk. nr. {{ bankAccount.accountNumber|default('') }}\n"
{% endfor %}
{% if invoice.customer %}
TEXT "------------------------------------------------\n"
ESC ! 0x08
TEXT "Pirkėjas: {{ invoice.customer.name|default('')|raw }}\n"
ESC ! 0x00
{% if invoice.customer.registrationCode %}
TEXT "Įmonės kodas: {{ invoice.customer.registrationCode }}\n"
{% endif %}
{% if invoice.customer.vatCode %}
TEXT "PVM kodas: {{ invoice.customer.vatCode }}\n"
{% endif %}
{% if invoice.customer.invoiceAddress %}
TEXT "Adresas: {{ invoice.customer.invoiceAddress|striptags|raw }}\n"
{% endif %}
{% endif %}
TEXT "================================================\n"
{% set vatGroups = {} %}
{% for record in records %}
{% set discountPct = record.orderProduct.discount|default(0) %}
{% set base = record.price.amount * record.quantity * (1 - discountPct / 100) %}
{% set pct = record.vat ? record.vat.percentage : 0 %}
{% set key = 'v' ~ pct %}
{% set prev = vatGroups[key]|default({'pct': pct, 'base': 0, 'vat': 0}) %}
{% set vatGroups = vatGroups|merge({(key): {'pct': pct, 'base': prev.base + base, 'vat': prev.vat + base * pct / 100}}) %}
{% if not summaryName %}
TEXT "{{ loop.index }}. {{ record.name|raw }}\n"
TEXT "{{ m.row(' ' ~ record.quantity|number_format(0, '.', '') ~ ' ' ~ record.unit ~ ' x ' ~ record.price.amount|number_format(2, '.', '') ~ ' PVM ' ~ pct ~ '%', record.sum.amount|number_format(2, '.', '')) }}\n"
{% if discountPct > 0 %}
TEXT "{{ m.row(' Nuolaida ' ~ discountPct ~ '%', '-' ~ (record.price.amount * record.quantity * discountPct / 100)|number_format(2, '.', '')) }}\n"
{% endif %}
{% endif %}
{% endfor %}
{% if summaryName %}
{% for group in vatGroups %}
TEXT "{{ m.row(summaryName ~ (vatGroups|length > 1 ? ' (PVM ' ~ group.pct ~ ' %)' : ''), group.base|number_format(2, '.', '')) }}\n"
{% endfor %}
{% endif %}
{% if invoice.includeTransportCosts %}
TEXT "{{ m.row(invoice.transportPriceText|default('Transportas'), invoice.transportPrice.amount|number_format(2, '.', '')) }}\n"
{% endif %}
TEXT "------------------------------------------------\n"
{% if not summaryName %}
TEXT "{{ m.row('Kiekis iš viso', invoice.quantity|number_format(0, '.', '')) }}\n"
{% endif %}
TEXT "{{ m.row('Suma be PVM, ' ~ cur, invoice.sum.amount|number_format(2, '.', '')) }}\n"
{% if invoice.discount.amount|default(0) > 0 %}
TEXT "{{ m.row('Nuolaida, ' ~ cur, invoice.discount.amount|number_format(2, '.', '')) }}\n"
{% endif %}
{% for group in vatGroups %}
TEXT "{{ m.row('Apmokestinama PVM ' ~ group.pct ~ ' %', group.base|number_format(2, '.', '')) }}\n"
{% if group.vat > 0 %}
TEXT "{{ m.row('Iš viso ' ~ group.pct ~ ' % PVM', group.vat|number_format(2, '.', '')) }}\n"
{% endif %}
{% endfor %}
ESC ! 0x08
TEXT "{{ m.row('Iš viso su PVM, ' ~ cur, invoice.sumLocalCurrencyWithVat.amount|number_format(2, '.', '')) }}\n"
ESC ! 0x00
{% if paymentTypes|length %}
TEXT "------------------------------------------------\n"
{% for payment in paymentTypes %}
TEXT "{{ m.row(payment.name|default(''), payment.sum.amount|number_format(2, '.', '')) }}\n"
{% endfor %}
{% endif %}
TEXT "------------------------------------------------\n"
{% if invoice.checkNumber %}
TEXT "Kasos kvito Nr. {{ invoice.checkNumber }}{% if order and order.finishedAt %}, išduotas {{ order.finishedAt|date('Y-m-d') }}{% endif %}\n"
{% endif %}
TEXT "Sąskaitą išrašė: {{ invoice.createdBy ? invoice.createdBy.name|raw : '' }}\n"
ESC d 2
TEXT "Priėmė ____________________________\n"
ESC d 5
GS V 1
Tip: Templates 3 and 4 differ only in the line {% set summaryName = '' %} near the top. Write any text between the quotes, for example {% set summaryName = 'Food service' %}, to print that text instead of the product list. Leave it empty ('') to print every product. You can create both templates and choose the right one when writing the invoice in POS.
Commands |
Each line of the template is one command. Lines that are not commands are skipped.
| Command | What it does |
ESC @ | Resets the printer. Put it on the first line. |
ESC ! 0x00 | Text style: 0x00 normal, 0x08 bold, 0x10 double height, 0x20 double width, 0x30 double size, 0x38 bold double size. Double width fits only 24 characters per line. |
TEXT "..." | Prints text. End it with \n to start a new line. Data fields can be used inside, for example {{ invoice.number }}. |
ESC d 4 | Feeds the given number of empty lines. |
GS V 1 | Cuts the paper (GS V 0 is a full cut on printers that support it). |
Data fields are the same as in invoice templates, for example invoice, records, order, system, paymentTypes and checkNumber. Lines starting with {% %} (conditions and loops) are allowed and do not print anything themselves.
Tips |
- 58 mm paper: in templates 2, 3 and 4 change
48in the first line to32and shorten the dashed lines to 32 characters. - Lithuanian letters: text is sent in the Baltic code page (CP1257). If letters print incorrectly, print Template 1 and check the printer's code page settings.
- Error when writing the invoice: usually a typo in a data field or a missing
{% endif %}/{% endfor %}. Check the template code. - Quotes in text: to print a value that may contain
&or quotes (for example a customer name), add|raw:{{ invoice.customer.name|raw }}.
English