Print receipt in thermal printer using JavaScript, CSS & HTML
October 10, 2019 · 3 min · 596 words · Parzibyte
Table of Contents
In this post I will show you how to print a ticket or receipt by using JavaScript (client side) and a thermal printer. At the end we will have a great ticket that looks like this:
Obviously you can change the font family, size, logo and so on. And remember: we will be using only pure CSS, HTML & JavaScript, no frameworks; though you can use any framework to render the content before printing.
You can see the demo here, and the full code on my GitHub.
First we have to write the HTML to print the ticket, in this case I will use a table to render the contents in a better way. Also, I’m using a image taken from pixabay. The code looks like this:
We are only calling window.print to tell the browser to print the document. When we press it, depending on the browser, a window appears, and there we can select the printer and some features.
Note: if you want to hide the dialog, you can use my plugin.
I am using Chrome, the thermal printer I have is a Xprinter 58, please note that I am not using any margins, so we can take the full space:
And when I click “Print” Chrome sends the receipt to the printer and we have this:
Conclusion
In this way we can print anything by using a Thermal printer, CSS, HTML and JavaScript. The only issue is that we have to confirm the action, but I have developed the solution by using a plugin.
Si el post ha sido de tu agrado te invito a que me sigas para saber cuando haya escrito un nuevo post, haya
actualizado algún sistema o publicado un nuevo software.
Facebook
| X
| Instagram
| Telegram |
También estoy a tus órdenes para cualquier contratación en mi página de contacto