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.
First of all, investigate the printer settings and what characters can be printed with the receipt printer
pip install python-escpos
pip install Pillow
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.
p.text ("Hello World \ n ")
, the first position character disappears, so be careful.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"
Add the specified image as a logo and print it Add the following image to the data created earlier and print it.
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()
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