Thermal printer

Print diacritic text in thermal printer – ESC POS commands

In this post I will show you how to print spanish text, text with accents or ñ letters on a thermal printer. I have been searching for this for a long time and now I want to share it.

Printing spanish text or diacritict text is necessary because sometimes the customer’s names or data contains some chars out of the ASCII range.

I will show you how to do this with JavaScript directly from the browser, but once you understand it, you will be able to print from any programming language.

Setup environment

To make this work, you need a free plugin that I have developed and also you need to share your printer. I encourage you to read the next post first:

The next section assumes that you have already printed the “Hello world” receipt and your printer is working with the plugin as expected.

First: check if your printer accepts diacritict text out of the box

Some printers support diacritic text or accented text out of the box, you only have to print it. First we create a ConectorPluginV3 instance and then we call EscribirTexto which only write text to the printer:

const imprimirAcentosConTextoNormal = async (nombreImpresora) => {
    const conector = new ConectorPluginV3(null, "licencia opcional aquí");
    conector.Iniciar();
    conector.EscribirTexto("¡Gracias por su compra, María José!\nFeliz año nuevo");
    conector.Feed(1);
    const respuesta = await conector
        .imprimirEn(nombreImpresora);
    if (respuesta === true) {
        alert("Impreso correctamente");
    } else {
        alert("Error: " + respuesta);
    }
}

I have a GOOJPRT PT210 printer and it is capable of printing text with diacritics out of the box:

Accented text in thermal printer out of the box

But on the other hand I have a Xprinter-58 and when I try this example it prints chinese chars, so I have another solution below.

Forcing accented text in thermal printer

Now we will force the printer to print diacritic text by changing the charcode page. Remember that each printer is different and sometimes you will need to enable or disable certain things.

For example, when I want to print spanish text on my ESC POS Xprinter 58, I have to disable chinese char mode by calling DeshabilitarElModoDeCaracteresChinos and then call TextoSegunPaginaDeCodigos which generates encoded text through iconv.

The TextoSegunPaginaDeCodigos accepts the page number, encoding and text to encode. Then it prints the encoded text to the printer.

In my case, I have used page number 2 but instead of cp860 (as it should be), I specify cp850. With this, I have been able to print next text in my chinese printer:

cp850 con numero 2 ¿EL VELOZ MURCIÉLAGO
HINDÚ COMÍA FELIZ CARDILLO Y KIWI? ¡LA CIGÜEÑA TOCABA EL SAXOFÓN DETRÁS DEL
PALENQUE DE PAJA!.

cp850 con número 2 ¿el veloz murciélago hindú comía feliz
cardillo y kiwi? ¡la cigüeña tocaba el saxofón detrás del palenque de paja!.

And the code looks like this:

const imprimirAcentosConPaginaDeCodigos = async (nombreImpresora) => {
    const conector = new ConectorPluginV3(null, "licencia opcional aquí");
    conector.Iniciar();
    conector.DeshabilitarElModoDeCaracteresChinos(); // Recuerda que tal vez no necesites invocar a este método si tu impresora no es china
    conector.TextoSegunPaginaDeCodigos(2, "cp850", "cp850 con numero 2 ¿EL VELOZ MURCIÉLAGO HINDÚ COMÍA FELIZ CARDILLO Y KIWI? ¡LA CIGÜEÑA TOCABA EL SAXOFÓN DETRÁS DEL PALENQUE DE PAJA!.");
    conector.Feed(1);
    conector.TextoSegunPaginaDeCodigos(2, "cp850", "cp850 con numero 2 ¿el veloz murciélago hindú comía feliz cardillo y kiwi? ¡la cigüeña tocaba el saxofón detrás del palenque de paja!.");
    conector.Feed(1);
    const respuesta = await conector
        .imprimirEn(nombreImpresora);
    if (respuesta === true) {
        alert("Impreso correctamente");
    } else {
        alert("Error: " + respuesta);
    }
}

If it does not work for you, try another page number combinations. For example you can use charcode page 3 and a different encoding. Anyway, the result receipt is as follows:

Spanish text with thermal printer – Print accented text on Chinese printer ESC POS

Full example: sales ticket

Now we can see a real example. Let’s print a sales receipt on a thermal printer, which contains diacritic text:

Sales receipt printed on thermal printer – Contains accented text

The source code is as follows:

const imprimirTicketConAcentos = async (nombreImpresora) => {
    const conector = new ConectorPluginV3(URLPlugin);
    conector.EstablecerTamañoFuente(1, 1);
    conector.EstablecerEnfatizado(false);
    conector.EstablecerAlineacion(ConectorPluginV3.ALINEACION_CENTRO);
    conector.DescargarImagenDeInternetEImprimir("https://ssb.wiki.gallery/images/f/f7/SSBU_spirit_Cuphead.png", ConectorPluginV3.TAMAÑO_IMAGEN_NORMAL, 160);
    conector.Feed(1);
    conector.EscribirTexto("Parzibyte's blog\n");
    conector.EscribirTexto("Blog de un programador\n");
    conector.DeshabilitarElModoDeCaracteresChinos();
    // Recuerda que si tu impresora soporta acentos sin configuración adicional solo debes invocar a EscribirTExto
    conector.TextoSegunPaginaDeCodigos(2, "cp850", "Teléfono: 123456789\n");
    conector.EscribirTexto("Fecha/Hora: 2021-02-08 16:57:55\n");
    conector.EstablecerEnfatizado(true);
    conector.EscribirTexto("Cliente: ");
    conector.EstablecerEnfatizado(false);
    conector.TextoSegunPaginaDeCodigos(2, "cp850", "María José\n");
    conector.EscribirTexto("--------------------------------\n");
    conector.EscribirTexto("Audífonos HyperX\n");
    conector.EstablecerAlineacion(ConectorPluginV3.ALINEACION_DERECHA);
    conector.EscribirTexto("25 USD\n");
    conector.EscribirTexto("--------------------------------\n");
    conector.EscribirTexto("TOTAL: 25 USD\n");
    conector.EscribirTexto("--------------------------------\n");
    conector.EstablecerAlineacion(ConectorPluginV3.ALINEACION_CENTRO);
    conector.TextoSegunPaginaDeCodigos(2, "cp850", "¡Muchas gracias por su compra y feliz año nuevo 2021!");
    conector.Feed(4);
    conector.Corte(1);
    conector.CorteParcial();
    const respuesta = await conector
        .imprimirEn(nombreImpresora);
    if (respuesta === true) {
        alert("Impreso correctamente");
    } else {
        alert("Error: " + respuesta);
    }
}

There we are printing images, feeding paper, cutting, and so on. If you want to read the full list check the docs and also you can check the examples or the source code.

Custom character

Another alternative is to define a custom char in a 12×24 grid and then print it. You can design any character.

Learn how to do it in the next link:

About charcode pages

The code pages vary depending on the printer. When printing on the printer, it is not necessary to indicate the encoding, but I do it because I convert the characters using iconv.

If it doesn’t work with a specific code page, you should try combining the page number and name or consult the documentation of your printer.

By the way, in this case, we did it with JavaScript, but the plugin also works with Python, C#, Java and any language that speaks HTTP. The connectors are listed at: https://parzibyte.me/blog/2022/09/30/plugin-impresoras-termicas-version-3/

Remember that if you need help, you can send me a message at https://parzibyte.me/#contacto.


I am available for hiring if you need help! I can help you with your project or homework feel free to contact me.
If you liked the post, show your appreciation by sharing it, or making a donation

parzibyte

Freelancer programmer ready to work with you. Web, mobile and desktop applications. PHP, Java, Go, Python, JavaScript, Kotlin and more :) https://parzibyte.me/

Entradas recientes

Receipt designer for thermal printers – Free and open source

In the last months I have been working on a ticket designer to print on…

11 months hace

JavaScript: store and read files with the Origin Private File System

In this post you will learn how to use the Origin Private File System with…

1 year hace

JavaScript: download file with fetch

In this post you will learn how to download a file in the background using…

1 year hace

SQLite3 with vanilla JavaScript and OPFS – Hello world

In this post I will show you how to use SQLite3 directly in the web…

1 year hace

Python Thermal Printing: A Comprehensive Guide for Printing on Thermal Printers

In this tutorial, we'll explore how to effortlessly print receipts, invoices, and tickets on a…

1 year hace

Image printing on Thermal printer

When printing receipts on thermal printers (ESC POS) sometimes it is needed to print images…

1 year hace

Esta web usa cookies.