Thermal printer

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 thermal printer using Python. I’ll introduce you to a powerful plugin that I’ve developed, enabling seamless integration for Python-based applications.

With this plugin, you’ll gain the ability to effortlessly print tickets from Python, regardless of your preferred framework—whether it’s the terminal, Flask, Tkinter, Django, or any other Python-supported platform.

It provides extensive functionality, allowing you to print images, barcodes, text with accents, change fonts, rotate text, and much more, sending the ESC POS commands native to thermal printers.

Join me as we dive into the world of thermal printing using Python, empowering you to generate professional-quality prints effortlessly.

Setup environment

To make this work, you will need the plugin running in the background and your printer shared as explained in:

I encourage you to read it first and print the “Hello ESC POS printer” receipt even if it is in another programming language.

Python ESC POS connector

To print on a thermal printer from Python, we need a connector that acts as a link between Python and the plugin. I have programmed it myself, and it looks like this:

See the gist on github.

Note: You will need the requests package, which you can easily install with pip install requests.

What I showed you earlier is the connector. Your Python code uses the connector, and the connector communicates with the plugin to print on thermal printers. That’s it.

Hello Thermal printer from Python

So, in our file where we have our Python code, we are going to import the connector like this:

import ConectorPython

The connector should be in a file called ConectorPython.py. However, if you prefer not to import it, you can also copy and paste the entire code into your project. Just be careful not to get confused.

Now, you can obtain the list of printers like this:

See the gist on github.

That will return the list of printers. It is a list of strings and will be useful if you want to give the user the opportunity to choose their printer or perform similar tasks.

Now, let’s create an instance of ConectorV3 and print a “Hello, World” message:

See the gist on github.

Please note that your printer’s name must be specified in nombreImpresora.

Note: If the plugin is listening on a different port, remember that you can specify the URL when constructing the ConectorV3 object or when invoking obtenerImpresoras.

Python Example: Printing a Ticket on a Thermal Printer using ESC POS

Discover a complete example of printing a ticket on a thermal printer using Python and ESC POS.

In this sample code, the printer name is provided directly, assuming you have prior knowledge of the desired printer. However, you can modify the code to allow users to choose from a printer list for more flexibility.

See the gist on github.

It could be a little confusing at the beggining but once you know every function, you will be able to unlash your creativity and print any type of receipts.

When running that code, the output should be like this:

Python receipt printing – Using ESC POS Commands on thermal printer

As you can see, we are printing images on a thermal printer using Python, along with QR codes, barcodes, styled text, and custom characters.

Now, what remains is to explore the documentation on the plugin’s webpage to familiarize yourself with all the available methods and the full extent of its capabilities.

You can find the documentation on the plugin’s webpage: https://parzibyte.me/blog/en/2019/10/13/print-ticket-in-thermal-printer-directly-from-browser/

Also, you can try online examples made with JavaScript and then port them to Python. The plugin works the same and does not mind which programming language do you use: https://parzibyte.github.io/ejemplos-javascript-plugin-v3/


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/

Compartir
Publicado por
parzibyte

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…

4 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…

7 months hace

JavaScript: download file with fetch

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

7 months hace

SQLite3 with vanilla JavaScript and OPFS – Hello world

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

9 months hace

Image printing on Thermal printer

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

10 months hace

Print diacritic text in thermal printer – ESC POS commands

In this post I will show you how to print spanish text, text with accents…

10 months hace

Esta web usa cookies.