Print out messages recieved via HTTP POST (webhook) on a connected ESC/POS compatible printer - which are usually thermal receipt printers.
Flask - to recieve webhook requests
python-escpos - to communicate with ESC/POS compatible printers
- Download app.py or clone the repository
- Install the required python modules from pip:
flask
andpython-escpos
- Change the USB vendor and product IDs at the top of the code so that the program can connect to the printer if your printer uses serial than you will need to change part of the code - instructions are in a seperate file
This works with Uptime Kuma, and can be setup as a notification, so your printer can print out status reports. Currently image and barcode printing is limited to the Pipsta/AP1400, as it uses the Able Systems specific Single Dot Line graphics mode.
You can run it with the flask development server like this and it will work fine, but you probably shouldn't do this for production:
python3 -m flask run -h **your.devices.ip.address**
(you need to include -h to allow requests from your local network)
Allowing access to the internet is not recommended, my code isn't perfect and the Flask development server shouldn't be used in production!
_ To put the code into Pipsta/AP1400 mode, change the variable at the top _
Feature | AP1400 | Others |
---|---|---|
Singlepart messages | ✅ | ✅ |
Formatted Text | ✅ | ✅ |
Image printing | ✅ | ✅ |
Barcode printing | ✅ | ✅ |
To send a single part message to the printer make a post request to http://<Your device's IP address>:5000/webhook
with a json body with a msg
key like this:
POST /webhook HTTP/1.1
Host: 192.168.x.x:5000
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Length: 19
Origin: http://192.168.x.x:5000
{
"msg": "hi"
}
_ Multipart messages are now stable, but please let me know if you find any problems_ Multipart messages are a new feature being actively developed, however, if you use version 1 of the API, it should work perfectly, feel free to make an issue if you find any bugs.
An example JSON payload is this:
{
"multipart": true,
"api_version": 1,
"parts": ["image1", "text1", "text2", "barcode1"],
"image1": {"type": "image", "imagedata": "iVBORw0KGgoAAAANSUhEUgAAAYAAAAA/AQMAAADT4o3oAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAGUExURQAAAP///6XZn90AAAAJcEhZcwAADsMAAA7DAcdvqGQAAAORSURBVEjH5Za9btRAEMfHcaJDCZJzEoh0puYJKKI4hREvEGpegQaJIopX4kVSokuRR7jlHUBpV6ILjZGIOEU+D/Ox693zXRIQJatEs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5j4B/osApTjxS+ABwU54fvMngsEubQ8LltNp81eCUfsPBYi/AREiYK1CRJO2AAAAAElFTkSuQmCC"},
"text1": {"type": "text", "formatting": false, "text": "This text is unformatted"},
"text2": {"type": "text", "formatting": true, "underlined": true, "inverted": true, "text": "This text is underlined"},
"barcode1": {"type": "barcode", "barcode-type": "UPC-A", "code": "12345678910"}
}
The key multipart
, when set to true, signifies a multipart, if the message is multipart, the api_version must be set, and be 1 or higher.
The key parts
should be a list of all the parts of the message, in the order you want them printed.
There should be a key corresponding to every item in the parts
list.
An image key is made up of two parts type
which should be set to image
so that the program so that the correct code is selected, and image
which should be the raw bytes of the image file, encoded as base64. Any image file that can be opened by Pillow is supported., currently image printing, as far as I know, will only work properly with an AP1400/Pipsta printer. If you want image printing to work with a different printer, you could modify the code to use python-escpos's built in image functions.
A barcode key has the "type": "barcode"
property and the barcode-type
property which should be one of:
- UPC-A
- UPC-E
- EAN-13
- EAN-8
- Code 39
- Int2 of 5
- Code 128A
- Code 128B
- Code 128C
- Code 93
And a
code
key which can be numeric or alphanumeric depending on the barcode type
A text key can be formatted or unformatted. A formatted key can be underlined
or inverted
(upside down). If the formatted
key is false
then the text will print normally. Both formatted and unformatted text will wrap onto the nextline
You may add the following types of formatting to your messages:
- Underlines
- Inverted Text
Pipsta - a rebadged Able Systems ap1400 designed for Raspberry Pis
Used it with another printer? Make an issue or a pull request!