odoo16中直连局域网zebra标签打印机打印二维码初版测试成功
This commit is contained in:
@@ -4,6 +4,7 @@ import logging
|
||||
import base64
|
||||
import qrcode
|
||||
import io
|
||||
import socket
|
||||
|
||||
import requests
|
||||
|
||||
@@ -388,7 +389,6 @@ class ShelfLocation(models.Model):
|
||||
record.product_id = False
|
||||
# record.location_status = '空闲'
|
||||
|
||||
|
||||
# 调取获取货位信息接口
|
||||
def get_sf_shelf_location_info(self):
|
||||
# sf_sync_config = self.env['res.config.settings'].get_values()
|
||||
@@ -465,6 +465,52 @@ class Sf_stock_move_line(models.Model):
|
||||
else:
|
||||
record.lot_qr_code = False
|
||||
|
||||
def generate_zpl_code(self, code):
|
||||
# 初始化ZPL代码字符串
|
||||
zpl_code = "^XA\n"
|
||||
zpl_code += "^CW1,E:SIMSUN.TTF^FS\n"
|
||||
zpl_code += "^CI28\n"
|
||||
|
||||
# 设置二维码位置
|
||||
zpl_code += "^FO50,50\n" # 调整二维码位置,使其与资产编号在同一行
|
||||
zpl_code += f"^BQN,2,6^FDLM,B0093{code}^FS\n"
|
||||
|
||||
# 设置资产编号文本位置
|
||||
zpl_code += "^FO300,60\n" # 资产编号文本的位置,与二维码在同一行
|
||||
zpl_code += "^A1N,45,45^FD编码名称: ^FS\n"
|
||||
|
||||
# 设置{code}文本位置
|
||||
# 假设{code}文本需要位于资产编号和二维码下方,中间位置
|
||||
# 设置{code}文本位置并启用自动换行
|
||||
zpl_code += "^FO300,120\n" # {code}文本的起始位置
|
||||
zpl_code += "^FB500,4,0,L,0\n" # 定义一个宽度为500点的文本框,最多4行,左对齐
|
||||
zpl_code += f"^A1N,40,40^FD{code}^FS\n"
|
||||
|
||||
# 在{code}文本框周围绘制线框
|
||||
# 假设线框的外部尺寸为宽度500点,高度200点
|
||||
# zpl_code += "^FO300,110^GB500,200,2^FS\n" # 绘制线框,边框粗细为2点
|
||||
|
||||
zpl_code += "^PQ1,0,1,Y\n"
|
||||
zpl_code += "^XZ\n"
|
||||
return zpl_code
|
||||
|
||||
def send_to_printer(self, host, port, zpl_code):
|
||||
# 将ZPL代码转换为字节串
|
||||
print('zpl_code', zpl_code)
|
||||
zpl_bytes = zpl_code.encode('utf-8')
|
||||
print(zpl_bytes)
|
||||
|
||||
# 创建socket对象
|
||||
mysocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
try:
|
||||
mysocket.connect((host, port)) # 连接到打印机
|
||||
mysocket.send(zpl_bytes) # 发送ZPL代码
|
||||
print("ZPL code sent to printer successfully.")
|
||||
except Exception as e:
|
||||
print(f"Error with the connection: {e}")
|
||||
finally:
|
||||
mysocket.close() # 关闭连接
|
||||
|
||||
def print_qr_code(self):
|
||||
self.ensure_one() # 确保这个方法只为一个记录调用
|
||||
# if not self.lot_id:
|
||||
@@ -473,28 +519,35 @@ class Sf_stock_move_line(models.Model):
|
||||
qr_code_data = self.lot_qr_code
|
||||
if not qr_code_data:
|
||||
raise UserError("没有找到二维码数据。")
|
||||
# 生成ZPL代码
|
||||
zpl_code = self.generate_zpl_code(self.lot_id.name)
|
||||
# 设置打印机的IP地址和端口号
|
||||
host = "192.168.50.110"
|
||||
port = 9100
|
||||
# 发送ZPL代码到打印机
|
||||
self.send_to_printer(host, port, zpl_code)
|
||||
|
||||
# 生成下载链接或直接触发下载
|
||||
# 此处的实现依赖于你的具体需求,以下是触发下载的一种示例
|
||||
attachment = self.env['ir.attachment'].sudo().create({
|
||||
'datas': self.lot_qr_code,
|
||||
'type': 'binary',
|
||||
'description': '二维码图片',
|
||||
'name': self.lot_name + '.png',
|
||||
# 'res_id': invoice.id,
|
||||
# 'res_model': 'stock.picking',
|
||||
'public': True,
|
||||
'mimetype': 'application/x-png',
|
||||
# 'model_name': 'stock.picking',
|
||||
})
|
||||
# 返回附件的下载链接
|
||||
download_url = '/web/content/%s?download=true' % attachment.id
|
||||
base_url = self.env['ir.config_parameter'].sudo().get_param('web.base.url')
|
||||
return {
|
||||
'type': 'ir.actions.act_url',
|
||||
'url': str(base_url) + download_url,
|
||||
'target': 'self',
|
||||
}
|
||||
# # 生成下载链接或直接触发下载
|
||||
# # 此处的实现依赖于你的具体需求,以下是触发下载的一种示例
|
||||
# attachment = self.env['ir.attachment'].sudo().create({
|
||||
# 'datas': self.lot_qr_code,
|
||||
# 'type': 'binary',
|
||||
# 'description': '二维码图片',
|
||||
# 'name': self.lot_name + '.png',
|
||||
# # 'res_id': invoice.id,
|
||||
# # 'res_model': 'stock.picking',
|
||||
# 'public': True,
|
||||
# 'mimetype': 'application/x-png',
|
||||
# # 'model_name': 'stock.picking',
|
||||
# })
|
||||
# # 返回附件的下载链接
|
||||
# download_url = '/web/content/%s?download=true' % attachment.id
|
||||
# base_url = self.env['ir.config_parameter'].sudo().get_param('web.base.url')
|
||||
# return {
|
||||
# 'type': 'ir.actions.act_url',
|
||||
# 'url': str(base_url) + download_url,
|
||||
# 'target': 'self',
|
||||
# }
|
||||
|
||||
# # # 定义一个方法,用于根据序列号生成二维码
|
||||
# # @api.depends('lot_id')
|
||||
|
||||
Reference in New Issue
Block a user