Categorías: JavaScript

A plugin for printing in thermal printers directly from browser

In this post I will introduce to you a plugin for printing tickets, receipts or invoices on a thermal printer using client-side JavaScript even if your web page or application is on a cloud server.

This plugin allows you to print directly from browsers, without dialogs and without browsers compatibility issues because you only have to send a HTTP request to print silently and in the background.

As we know, printing a ticket with JavaScript, CSS & HTML is possible, but difficult for the end user, because you have to select the printer and confirm the printing.

Another disadvantage of printing with JavaScript without a plugin is that we cannot cut the paper, open the cash drawer or things like that.

That’s why I created the solution to the problem and I come to expose it. And no, it is not a misleading post.

New: use the free receipt designer

Now you can use this plugin with the brand new ESC POS thermal printer receipt maker: https://parzibyte.me/apps/ticket-designer/#/first-steps

A free plugin for printing in thermal printers directly from browser

That is why I have decided to create a plugin to print with JavaScript, without any dependency and using pure JavaScript client side.

If you want to jump to the examples or download the plugin, go to the docs.

What comes next are technical details of how the plugin works.

Sordid details following…

How does it work?

The operation is simple. The plugin, which is an executable, creates a web server on port 8000 (which only lives while the plugin is running) and listens for requests.

The plugin, unlike JavaScript, can interact with the user’s computer, so you just tell it what to do over HTTP.

JavaScript is really good at making HTTP requests through, for example, fetch.

The plugin behaviour is best illustrated with the above image.

Because the plugin speaks HTTP, a ticket can be printed from any programming language that can communicate with the plugin.

The library

Not all tickets are the same, and that is why I had to create a way to make the user print and modify editable and custom tickets.

To do this, I made an array where the actions are placed. Within the plugin I simply interpret them and send them to the printer.

Actions are an object that has a name (nombre) and arguments (argumentos), which is the data used for the action.

It looks best illustrated in this class:

See the gist on github.

 

All these actions are added to an array; through the methods that the ConectorPlugin class exposes: https://github.com/parzibyte/ejemplos-javascript-plugin-v3/blob/main/ConectorJavaScript.js

This class simply tries to validate the data a bit, but in the end, what is sent in the imprimirEn method is an array of objects.

On the server side that is also validated, and that’s all.

Hello world

To use this plugin, first you need to download it here: https://github.com/parzibyte/plugin-impresora-termica-v3/releases/latest

Then you need to install your printer and share it:

Now you can go and try the live examples: https://parzibyte.github.io/ejemplos-javascript-plugin-v3/hola.html

That’s all. If an example worked for you, then you can call the plugin from any language and check the examples source code. Refer to the examples in the previous link or check them in GitHub: https://github.com/parzibyte/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

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…

10 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

Esta web usa cookies.