调整打印代码结构
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
from . import jikimo_printing
|
||||||
from . import maintenance_printing
|
from . import maintenance_printing
|
||||||
from . import workorder_printing
|
from . import workorder_printing
|
||||||
|
|
||||||
|
|||||||
56
jikimo_printing/models/jikimo_printing.py
Normal file
56
jikimo_printing/models/jikimo_printing.py
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
from io import BytesIO
|
||||||
|
import qrcode
|
||||||
|
from reportlab.pdfgen import canvas
|
||||||
|
from reportlab.lib.pagesizes import A4
|
||||||
|
from PIL import Image
|
||||||
|
from reportlab.lib.utils import ImageReader
|
||||||
|
from odoo import models, fields, api
|
||||||
|
|
||||||
|
class JikimoPrinting(models.AbstractModel):
|
||||||
|
_name = 'jikimo.printing'
|
||||||
|
|
||||||
|
def print_qr_code(self, data):
|
||||||
|
"""
|
||||||
|
打印二维码
|
||||||
|
"""
|
||||||
|
# 生成二维码
|
||||||
|
qr = qrcode.QRCode(version=1, box_size=10, border=5)
|
||||||
|
qr.add_data(data)
|
||||||
|
qr.make(fit=True)
|
||||||
|
qr_image = qr.make_image(fill_color="black", back_color="white")
|
||||||
|
|
||||||
|
# 将PIL Image转换为reportlab可用的格式
|
||||||
|
temp_image = BytesIO()
|
||||||
|
qr_image.save(temp_image, format="PNG")
|
||||||
|
temp_image.seek(0)
|
||||||
|
|
||||||
|
# 创建PDF
|
||||||
|
pdf_buffer = BytesIO()
|
||||||
|
c = canvas.Canvas(pdf_buffer, pagesize=A4)
|
||||||
|
|
||||||
|
# 计算位置
|
||||||
|
a4_width, a4_height = A4
|
||||||
|
qr_width = 200
|
||||||
|
qr_height = 200
|
||||||
|
x = (a4_width - qr_width) / 2
|
||||||
|
y = (a4_height - qr_height) / 2
|
||||||
|
|
||||||
|
# 直接从BytesIO绘制图片
|
||||||
|
c.drawImage(ImageReader(Image.open(temp_image)), x, y, width=qr_width, height=qr_height)
|
||||||
|
c.save()
|
||||||
|
|
||||||
|
# 获取PDF内容并打印
|
||||||
|
pdf_content = pdf_buffer.getvalue()
|
||||||
|
printer = self.env['printing.printer'].get_default()
|
||||||
|
printer.print_document(report=None, content=pdf_content, doc_format='pdf')
|
||||||
|
|
||||||
|
# 清理资源
|
||||||
|
pdf_buffer.close()
|
||||||
|
temp_image.close()
|
||||||
|
|
||||||
|
def print_pdf(self, pdf_data):
|
||||||
|
"""
|
||||||
|
打印PDF
|
||||||
|
"""
|
||||||
|
printer = self.env['printing.printer'].get_default()
|
||||||
|
printer.print_document(report=None, content = pdf_data, doc_format='pdf')
|
||||||
@@ -7,9 +7,6 @@ class MaintenancePrinting(models.Model):
|
|||||||
|
|
||||||
print('self.name========== %s' % self.name)
|
print('self.name========== %s' % self.name)
|
||||||
self.ensure_one()
|
self.ensure_one()
|
||||||
qr_code_data = self.qr_code_image
|
|
||||||
if not qr_code_data:
|
|
||||||
raise UserError("没有找到二维码数据。")
|
|
||||||
# maintenance_equipment_id = self.id
|
# maintenance_equipment_id = self.id
|
||||||
# # host = "192.168.50.110" # 可以根据实际情况修改
|
# # host = "192.168.50.110" # 可以根据实际情况修改
|
||||||
# # port = 9100 # 可以根据实际情况修改
|
# # port = 9100 # 可以根据实际情况修改
|
||||||
@@ -23,9 +20,9 @@ class MaintenancePrinting(models.Model):
|
|||||||
# self.print_qr_code(maintenance_equipment_id, host, port)
|
# self.print_qr_code(maintenance_equipment_id, host, port)
|
||||||
|
|
||||||
# 切换成A4打印机
|
# 切换成A4打印机
|
||||||
printer = self.env['printing.printer'].get_default()
|
|
||||||
try:
|
try:
|
||||||
printer.print_document(report=None, content = qr_code_data)
|
self.env['jikimo.printing'].print_qr_code(self.id)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise UserError(f"打印失败: {str(e)}")
|
raise UserError(f"打印失败: {str(e)}")
|
||||||
|
|
||||||
|
|||||||
@@ -19,9 +19,7 @@ class MrpWorkorder(models.Model):
|
|||||||
if pdf_data:
|
if pdf_data:
|
||||||
try:
|
try:
|
||||||
# 执行打印
|
# 执行打印
|
||||||
printer = self.env['printing.printer'].get_default()
|
self.env['jikimo.printing'].print_pdf(pdf_data)
|
||||||
printer.print_document(report=None, content = pdf_data, doc_format='pdf')
|
|
||||||
|
|
||||||
wo.production_id.product_id.is_print_program = True
|
wo.production_id.product_id.is_print_program = True
|
||||||
_logger.info(f"工单 {wo.name} 的PDF已成功打印")
|
_logger.info(f"工单 {wo.name} 的PDF已成功打印")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user