文章目录
- 简介
- windows
- linux
- 安装cups步骤
简介
打印机python实现
windows
import win32ui, win32con
hDC = win32ui.CreateDC()
hDC.CreatePrinterDC('ZDesigner ZD421CN-300dpi ZPL') # 缺省将使用默认打印机
hDC.StartDoc("title") # 新建打印任务设置名称
hDC.StartPage() # 开始一个页面
DataList = [
[15 , 14 , '字体字号,宋体 200' , {'name': '宋体', 'height': 200}],
[15 , 450 , '一整行逆时针倾斜9度' , {'name': '宋体', 'height': 150, 'escapement' : 90 }],
[15 , 800 , '这个字体旋转好像没用', {'name': '黑体', 'height': 180, 'orientation': 90}],
[1515,1000, '一整行顺时针倾斜90度', {'name': '宋体', 'height': 150, 'escapement' :-900}],
[15 , 1200, '字体加粗1000' , {'name': '宋体', 'height': 150, 'weight' : 1000}],
[15 , 1580, '斜体字' , {'name': '宋体', 'height': 150, 'italic' : 1}],
[15 , 1980, '下划线' , {'name': '宋体', 'height': 150, 'underline' : 1}],
[15 , 2380, '删除线' , {'name': '宋体', 'height': 150, 'strike out' : 1}]
]
for data in DataList:
font = win32ui.CreateFont(data[3]) # 设置字体
hDC.SelectObject(font) # 将字体对象应用于设备上下文
hDC.TextOut(data[0], data[1], data[2]) # 写入文本
fontdict = {
'name' : '宋体', # 字体名称 str
'height': 150, # 字体高 int
'width' : 150, # 字体宽 int
'escapement' : 0, # 字符串倾斜角度 int
'orientation': 0, # 字符旋转角度 int
'weight' : 600, # 字体的粗细 int
'italic' : 0, # 倾斜 byte 0或1
'underline' : 0, # 下划线 byte
'strike out' : 0, # 删除线 byte
'charset': win32con.DEFAULT_CHARSET, # 字符集 int
'out precision' : win32con.OUT_DEFAULT_PRECIS, # 输出精度 int
'clip precision': win32con.CLIP_DEFAULT_PRECIS, # 剪切精度 int
'quality' : win32con.DEFAULT_QUALITY, # 输出质量 int
'pitch and family': win32con.DEFAULT_PITCH | win32con.FF_DONTCARE, # 字体间距和字体集 int
}
# 创建一个字体对象
font = win32ui.CreateFont(fontdict)
hDC.SelectObject(font)
hDC.TextOut(15, 2800, "Hello, World!测试")
hDC.EndPage() #关闭页面
hDC.EndDoc() #关闭任务
linux
import cups
import os
import cv2
import numpy as np
import qrcode
import datetime
import codecs
from PIL import Image, ImageDraw, ImageFont
def draw_box_string(img, x, y, font_size, string):
"""
img: imread读取的图片;
x,y:字符起始绘制的位置;
string: 显示的文字;
return: img
"""
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = Image.fromarray(img)
draw = ImageDraw.Draw(img)
# simhei.ttf 是字体,你如果没有字体,需要下载
font = ImageFont.truetype("SimHei.ttf", font_size, encoding="utf-8")
# font = ImageFont.truetype("simsun.ttc", font_size, encoding="utf-8")
# font = ImageFont.truetype("simkai.ttf", font_size, encoding="utf-8")
# font.set_variation_by_name("bold")
# draw.text((x, y - font_size), string, (0, 0, 0), font=font)
#加粗
draw.text((x, y - font_size), string, (0, 0, 0), font=font ,stroke_width=1, stroke_fill="black")
img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
return img
def print_qr():
# 创建一个CUPS连接
conn = cups.Connection()
# 获取所有打印机
printers = conn.getPrinters()
# 打印所有打印机的名称
for printer in printers:
print(printer)
# 选择一个打印机
#printer_name = list(printers.keys())[0]
#printer_name = "ZTC-ZD421CN-300dpi-ZPL"
printer_name = "Zebra_Technologies_ZTC_ZD421CN-300dpi_ZPL"
# print_width = 200
# print_height = 100
# qrcode_size = 70
# font_size = 10
# padding_bottom = 6
# print_width = 500
# print_height = 300
# qrcode_size = 210
# font_size = 28
# made_in_china_font_size = 20
# padding_bottom = 18
print_width = 1000
print_height = 600
qrcode_size = 420
font_size = 56
made_in_china_font_size = 40
padding_bottom = 36
img_file_name = "qrcode.png"
pdf_file_name = "qrcode.pdf"
canvas = np.ones((print_height,print_width,3), np.uint8)
canvas[:] = (255, 255, 255)
now = datetime.datetime.now()
str_time = now.strftime("%Y%m%d")
count = 1
result = str(count).zfill(4)
line1='S56EV左前门护板合件总成(浅色高配)'
line2='403008124AA'
line3='8MG'
line4='202401160013'
line4=str_time+result
qrcode_content = '10#'+line2+'$11#'+line3+'$12#'+line4+'$'
print(qrcode_content)
img = qrcode.make(qrcode_content)
img.save(img_file_name)
img_qrcode = cv2.imread(img_file_name)
img_qrcode = cv2.resize(img_qrcode, (qrcode_size, qrcode_size))
canvas = draw_box_string(canvas, 10, int(print_height/4)-padding_bottom, font_size, line1)
canvas = draw_box_string(canvas, 10, int(print_height/4*2)-padding_bottom, font_size+8, line2)
canvas = draw_box_string(canvas, 10, int(print_height/4*3)-padding_bottom -40, font_size+8, line3)
canvas = draw_box_string(canvas, 10, int(print_height/4*4)-padding_bottom -40*2, font_size+8, line4)
qrcode_x1= print_width - qrcode_size - padding_bottom
qrcode_y1= int(print_height/4) - padding_bottom
qrcode_x2=qrcode_x1+qrcode_size
qrcode_y2=qrcode_y1+qrcode_size
canvas[qrcode_y1:qrcode_y2,qrcode_x1:qrcode_x2] = img_qrcode
made_in_china = "MADE IN CHINA"
canvas = draw_box_string(canvas, qrcode_x1+85, qrcode_y2+10, made_in_china_font_size, made_in_china)
# 逆时针旋转90度
# canvas = cv2.rotate(canvas, cv2.ROTATE_90_COUNTERCLOCKWISE)
cv2.imwrite(img_file_name, canvas)
# 转pdf
pil_img = Image.open(img_file_name)
pil_img.save(pdf_file_name, "pdf", save_all=True)
#file_path = img_file_name
file_path = pdf_file_name
#file_path = "test_print_qrcode.pdf"
#file_path = "test_print.txt"
# lp -d Zebra_Technologies_ZTC_ZD421CN-300dpi_ZPL -o media=5x3cm -o fit-to-page -o orientation-requested=4 -o print-quality=5 qrcode.pdf
# 设置打印方向为"正面朝上"(landscape)或者"背面朝上"(reverse-landscape)
# orientation = "portrait" # 可选值有:portrait、landscape、reverse-portrait、reverse-landscape
# if os.path.isfile(file_path):
# print("print:",file_path)
# # lp -d Zebra_Technologies_ZTC_ZD421CN-300dpi_ZPL -o media=5x3cm -o fit-to-page -o orientation-requested=4 -o print-quality=5 -o page-left=10 qrcode.pdf
# conn.printFile(printer_name, file_path, "Python Print Job", {})
# else:
# print("File not found")
print_qr()
安装cups步骤
CUPS 默认安装在 Ubuntu 桌面中。 要在 Ubuntu 服务器上安装 CUPS,请输入以下命令:
sudo apt install cups
安装CUPS打印服务器后,通过以下命令启动CUPS打印服务:
sudo systemctl start cups
sudo systemctl enable cups
sudo systemctl status cups
步骤 3. 在 Ubuntu 上配置 CUPS。
现在我们编辑 CUPS 主配置文件:
sudo nano /etc/cups/cupsd.conf
首先查找该行:
Browsing Off
将该行更改为:
Browsing On
接下来,找到“仅侦听来自本地计算机的连接”部分。 在这里,将有一个标题为“Listen localhost:631”的条目。 将其更改为“端口 631”:
#Listen localhost:631
Port 631
我们还需要确保 CUPS 正在侦听所有接口。 为此,请查找以下部分:
Order allow,deny 将上面的部分更改为: Order allow,deny Allow @LOCAL 另外,将其添加为 /admin 允许从本地网络进行远程管理的目录:
<Location /admin>
Order allow,deny
将该部分更改为:
<Location /admin>
AuthType Default
Require valid-user
Order allow,deny
Allow @LOCAL
Save 和 close 文件。 然后重新启动 CUPS 以使更改生效:
sudo systemctl restart cups
配置 CUPS 后,现在我们将确保使用 Bonjour 和 IPP 协议将打印机共享到您的网络。 首先,我们需要使用以下命令安装 avahi-daemon:
sudo apt install avahi-daemon
安装 Avahi-daemon 后,使用以下命令启动和自动引导时间:
sudo systemctl start avahi-daemon
sudo systemctl enable avahi-daemon
步骤 4. 配置防火墙。
如果您启用了防火墙,请确保允许客户端的机器与 CUPS 端口 631 通信:
sudo ufw allow 631/tcp
sudo ufw allow 5353/udp
步骤 5. 连接到打印机。
现在我们添加的打印机将取决于您使用的桌面操作系统。 为了 example,使用 Ubuntu Linux,我可以从 Settings | 打开 Add Printer 窗口。 设备和新打印机会自动出现。
此时,您的打印机应该已添加到桌面并可以打印。 如果需要,您可以通过将浏览器指向基于 Web 的控制台来管理打印机和服务器 https://your-server-ip-addrees:631. 这 admin 控制台在地址 https://your-server-ip-address:631/admin. 为了 admin 控制台,系统会提示您输入有效的用户名和密码。
最后需要在浏览器中创建打印机,使用命令获得设备名,如:
“Zebra_Technologies_ZTC_ZD421CN-300dpi_ZPL”
打印命令