Operate a receipt printer with python

Related article

Be in trouble

First of all, problem definition! I've tried using the task management tool many times and failed repeatedly, but I think it's because it's a hassle to open each task on my smartphone, so I can't see it physically. , I thought that I should print it with a printer, so I created these.

function

What to use

design

1. Settings and coding 1

First of all, investigate the printer settings and what characters can be printed with the receipt printer

Install the library used by python

pip install python-escpos
pip install Pillow

I used a free font as the font.

Free font site

Printing with a printer

from escpos.printer import Usb
from escpos import *

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont

#Printing using a receipt printer

width = 500
height = 250

image = Image.new('1', (width, height), 255)
draw = ImageDraw.Draw(image)
font = ImageFont.truetype('FLOPDesignFont.ttf', 50, encoding='unic')
draw.text((0, 0), "Japanese" + " ", font=font, fill=0)
font = ImageFont.truetype('FLOPDesignFont.ttf', 28, encoding='unic')
draw.text((0, 82), "abcdefghijklmnopqrstuvwxyz", font=font, fill=0)
draw.text((0, 112), "ABCDEFGHIJKLMNOPQRSTUVWXYZ", font=font, fill=0)
draw.text((0, 142), "1234567890" + " ", font=font, fill=0)
draw.text((0, 172), "!\"#$%&'()=~|+*<>?_{}" + " ", font=font, fill=0)

p = Usb(0x0416, 0x5011, 0, 0x81, 0x03)
p.text("Hello World\n")
p.image(image)
p.cut()

The printed version is as follows.

IMG_20200620_103642.jpg

USB settings

If you want to print with a receipt printer, you need to set this. I will confirm it.

p = Usb(0x0416, 0x5011, 0, 0x81, 0x03)

First, let's run lsusb

$ lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 006: ID 8087:07dc Intel Corp. 
Bus 001 Device 005: ID 0bda:579a Realtek Semiconductor Corp. 
Bus 001 Device 004: ID 0bda:0129 Realtek Semiconductor Corp. RTS5129 Card Reader Controller
Bus 001 Device 003: ID 0bda:b812 Realtek Semiconductor Corp. 
Bus 001 Device 008: ID 0416:5011 Winbond Electronics Corp. Virtual Com Port
Bus 001 Device 002: ID 062a:4102 Creative Labs 
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

The following are connected to the receipt printer. Bus 001 Device 008: ID 0416: 5011 Winbond Electronic's s Corp. Virtual Com Port This 0416:5011 part is the first and second Usb arguments.

You can print by adding sudo and running python with root privileges. At this time, add a rule to udev so that general users can access this device, and load the udev rule.

udev Simply put, this USB manages something even if you plug or unplug a USB device. (Please check the link to be exact.)

udev settings Create a /etc/udev/rules.d/10-printer.rules file and write the following: If the device ID: 0416: 5011 is different in the previous lsusb, please also match this.

SUBSYSTEM=="usb", ATTR{idVendor}=="0416", ATTR{idProduct}=="5011", MODE="0666", GROUP="plugdev"

2. Settings and coding 2

Add the specified image as a logo and print it Add the following image to the data created earlier and print it.

qiita-logo.png

from escpos.printer import Usb
from escpos import *

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont

#Printing using a receipt printer

width = 500
height = 400

image = Image.new('1', (width, height), 255)
draw = ImageDraw.Draw(image)
font = ImageFont.truetype('FLOPDesignFont.ttf', 50, encoding='unic')
draw.text((0, 100), "Japanese" + " ", font=font, fill=0)
font = ImageFont.truetype('storage/font/FLOPDesignFont.ttf', 28, encoding='unic')
draw.text((0, 182), "abcdefghijklmnopqrstuvwxyz", font=font, fill=0)
draw.text((0, 212), "ABCDEFGHIJKLMNOPQRSTUVWXYZ", font=font, fill=0)
draw.text((0, 242), "1234567890" + " ", font=font, fill=0)
draw.text((0, 272), "!\"#$%&'()=~|+*<>?_{}" + " ", font=font, fill=0)

logo = Image.open('qiita-logo.png')

image.paste(logo, (150, 10))

# image.show()
p = Usb(0x0416, 0x5011, 0, 0x81, 0x03)
p.text(" \n")
p.image(image)
p.cut()

IMG_20200620_114940.jpg

Summary

You can now print text and simple images on your receipt printer. By the way, I chose the receipt printer this time simply because the price is cheap. The receipt printer costs 4500 yen / 5 cash register papers cost about 900 yen!

Recommended Posts

Operate a receipt printer with python
Print memos with a receipt printer (serialization)
Operate Kinesis with Python
Operate Blender with Python
Operate Excel with Python (1)
Operate Excel with Python (2)
Operate Excel with Python openpyxl
Operate TwitterBot with Lambda, Python
[Note] Operate MongoDB with Python
Using a printer with Debian 10
Make a fortune with Python
[Python] [SQLite3] Operate SQLite with Python (Basic)
Create a directory with python
[Python] What is a with statement?
Solve ABC163 A ~ C with Python
A python graphing manual with Matplotlib.
Let's make a GUI with python.
Try to operate Facebook with Python
Solve ABC166 A ~ D with Python
Create a virtual environment with Python!
I made a fortune with Python.
Building a virtual environment with Python 3
Solve ABC168 A ~ C with Python
Make a recommender system with python
Operate ECHONET Lite appliances with Python
[Python] Generate a password with Slackbot
Solve ABC162 A ~ C with Python
Solve ABC167 A ~ C with Python
Solve ABC158 A ~ C with Python
Let's make a graph with python! !!
[Python] Inherit a class with class variables
I made a daemon with Python
Write a batch script with Python3.5 ~
[Python] A memo to operate ROM created by GBDK with PyBoy
[Pyenv] Building a python environment with ubuntu 16.04
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
Create a Python function decorator with Class
Creating a simple PowerPoint file with Python
Building a Python3 environment with Amazon Linux2
Let's make a shiritori game with Python
Install Python as a Framework with pyenv
Build a blockchain with Python ① Create a class
Add a Python data source with Redash
Create a dummy image with Python + PIL.
I made a character counter with Python
[Python] Drawing a swirl pattern with turtle
Operate smartlife power supply with python (de-IFTTT)
I drew a heatmap with seaborn [Python]
[Python] Create a virtual environment with Anaconda
Let's create a free group with Python
A memo with Python2.7 and Python3 on CentOS
Building a Python 3.6 environment with Windows + PowerShell
Map rent information on a map with python
Search the maze with the python A * algorithm
Daemonize a Python web app with Supervisor
Let's make a voice slowly with Python
Created a darts trip with python (news)
[GCP] Operate Google Cloud Storage with Python
I tried a functional language with Python
[Python] A quick web application with Bottle!
[AtCoder] Solve ABC1 ~ 100 A problem with Python