You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

339 lines
11 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

"""
此文件是用来生成Code128条形码 并实现打印功能
"""
import os
import uuid
import unicodedata
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtPrintSupport import QPrinter
from PyQt5.QtWidgets import QApplication
from pystrich.code128 import Code128Encoder
from conf import setting
class CreateBarcode:
"""
Code128Encoder(options={}) options参数
* ttf_font用于呈现标签的truetype字体文件的绝对路径
* ttf_fontsize绘制标签的字体大小
* label_border条形码和标签之间的像素空间数
* bottom_border标签和底部边框之间的像素空间数
* height图像的高度以像素为单位
* show_label是否在条形码下面显示标签默认为True“”
"""
code_path = os.path.join(os.getcwd(), "img")
if not os.path.exists(code_path):
os.umask(0)
os.makedirs(code_path)
def __init__(self):
self.archive_name = ""
def create_Code128_img(self, barcode, name='', manufacturer=''):
show_barcode_only = False
if self.archive_name == "耗材" or (self.archive_name == "普通试剂" and setting.BARCODE_COMMON_SHOW_ONLY):
show_barcode_only = True
a = Code128Encoder(
barcode,
options={
'ttf_font': os.path.join(os.getcwd(), 'arial.ttf'),
'label_border': 0,
'height': 140,
'bottom_border': 14,
'ttf_fontsize': 46 if show_barcode_only else 20,
},
)
# bar_width 条码宽度尺寸
file_name = str(uuid.uuid4()) + '.png'
a.save(file_name, bar_width=3)
if show_barcode_only:
self.printer_only_code(file_name)
else:
self.printer_code(file_name, name, manufacturer)
def printer_code(self, file_name, name, manufacturer):
a = QApplication([])
document = QTextDocument()
document.setDocumentMargin(0)
fontId = QFontDatabase.addApplicationFont(os.path.join(os.getcwd(), 'simsun.ttf'))
if len(name) >= 6:
name = name[:6] + '..'
if manufacturer and len(manufacturer) >= 6:
manufacturer = manufacturer[:6] + '..'
table_html = """
<div style='display:flex;width:100%'>&nbsp;&nbsp;</div>
<img src="{0}"><br/>
&nbsp; 试剂名称:{1} <br/>
&nbsp; 生产厂家:{2} <br/>
&nbsp; 开封时间: <br/>
&nbsp; 开封人:
""".format(
file_name, name, manufacturer
)
html = """
<head>
<title>Report</title>
<style>
table {{
width: 100%;
border-collapse: collapse;
}}
td {{
border: 1px solid black;
padding: 2px;
text-align: center;
font-size: 18px;
}}
div,br,p,span {{
white-span: nowrap;
word-warp: break-word;
overflow:hidden;
text-overflow: ellipsis;
}}
img {{
display: block;
margin: 0 auto;
}}
</style>
</head>
<body style='font-family:WenQuanYi Micro Hei;font-size:28px;line-height:22px;font-weight: bold;'>
{}
</body>
""".format(
table_html
)
document.setHtml(html)
printer = QPrinter()
printer.setPageSize(QPagedPaintDevice.Custom)
printer.setPaperSize(QSizeF(45.0, 30.0), QPrinter.Millimeter)
# 设置纸张到条码的边距 左上下右
printer.setPageMargins(5, 3, 0, 0, QPrinter.Millimeter)
document.setPageSize(QSizeF(400.0, 320.0))
print(document.pageSize(), printer.resolution(), printer.pageRect())
print('正在打印中。。。。')
document.print_(printer)
print('打印完成。。')
os.remove(file_name)
def create_drug_lobel_code(self, **kwargs):
a = Code128Encoder(
# kwargs.get("code_number"),
"111",
options={
'ttf_font': os.path.join(os.getcwd(), 'arial.ttf'),
'label_border': 0,
'height': 15,
'bottom_border': 0,
'ttf_fontsize': 0,
},
)
# bar_width 条码宽度尺寸
file_name = os.path.join(self.code_path, str(uuid.uuid4()) + '.png')
print(file_name)
a.save(file_name, bar_width=1)
kwargs["file_path"] = file_name
self.printer_drug_label(**kwargs)
def zhsy_create_drug_label_code(self, **kwargs):
"""
珠海食药打印配液标签
:param kwargs:
:return:
"""
a = Code128Encoder(
kwargs.get("code_number"),
options={
'ttf_font': os.path.join(os.getcwd(), 'arial.ttf'),
'label_border': 0,
'height': 15,
'bottom_border': 0,
'ttf_fontsize': 0,
},
)
file_name = os.path.join(self.code_path, str(uuid.uuid4()) + '.png')
a.save(file_name, bar_width=1)
kwargs["file_path"] = file_name
self.zhsy_printer_drug_label(**kwargs)
def printer_drug_label(self, **kwargs):
a = QApplication([])
document = QTextDocument()
# <p style="font-size:12px;">
html = """
<head>
<title>Report</title>
<style></style>
</head>
<body>
<div style="display:flex; align-content: center;width:100%">
<p style="font-size:6px; width: 100%;">
<span style="font-size:8px;width: 100%;">华润三九制药</span> <br />
试剂名称: {} <br />
级别: {} <br />
批号: {} <br />
编号: {} <br />
货位号: {} {} <br />
<img src="{}">
</p>
</div>
</body>
""".format(
kwargs.get("name"),
kwargs.get("purity"),
kwargs.get("standard_code"),
kwargs.get("remark12"),
kwargs.get("client_name"),
kwargs.get("flow_position_code"),
kwargs.get("file_path"),
)
document.setHtml(html)
printer = QPrinter()
printer.setPageSize(QPagedPaintDevice.Custom)
# printer.setPaperSize(QSizeF(60.0,40.0),QPrinter.Millimeter)
printer.setPaperSize(QSizeF(30.0, 18.0), QPrinter.Millimeter)
# printer.setPaperSize(QSizeF(30.0,50.0),QPrinter.Millimeter)
# 设置纸张到条码的边距 左上下右
printer.setPageMargins(6, 3, 0, 0, QPrinter.Millimeter)
# printer.setPageMargins(20, 20, 0, 0, QPrinter.Millimeter)
document.setPageSize(QSizeF(printer.pageRect().size()))
# document.setPageSize(QSizeF(50.0,30.0))
print('正在打印中。。。。')
document.print_(printer)
print('打印完成。。')
os.remove(kwargs.get("file_path"))
def zhsy_printer_drug_label(self, **kwargs):
print(kwargs.get("file_path"), 55555555)
print('sss:', kwargs)
a = QApplication([])
document = QTextDocument()
html = """
<head>
<title>Report</title>
<style></style>
</head>
<body>
<div>
<p style="font-size:11px;">
<span style="font-size:13px;">珠海市食品药品检验所</span> <br />
名称: {} <br />
编号: {} <br />
浓度: {} &nbsp;&nbsp;溶剂:{}<br />
配置日期: {} <br />
有 效 期:{} <br />
配 置 人:{} <br />
</p>
<img src="{}">
</div>
</body>
""".format(
kwargs.get("name")[:12],
kwargs.get("code_number"),
kwargs.get("purity")[:10],
kwargs.get("solvent"),
kwargs.get("start_time")[:10],
kwargs.get("end_time")[:10],
kwargs.get("user_name"),
kwargs.get("file_path"),
)
print(html)
document.setHtml(html)
printer = QPrinter()
printer.setPageSize(QPagedPaintDevice.Custom)
printer.setPaperSize(QSizeF(60.0, 40.0), QPrinter.Millimeter)
# printer.setPaperSize(QSizeF(30.0,50.0),QPrinter.Millimeter)
# 设置纸张到条码的边距 左上下右
printer.setPageMargins(6, 2, 0, 0, QPrinter.Millimeter)
document.setPageSize(QSizeF(printer.pageRect().size()))
print('正在打印中。。。。')
document.print_(printer)
print('打印完成。。')
def printer_only_code(self, file_name):
a = QApplication([])
document = QTextDocument()
document.setDocumentMargin(0)
fontId = QFontDatabase.addApplicationFont(os.path.join(os.getcwd(), 'simsun.ttf'))
table_html = """
<div style='display:flex;width:100%'>&nbsp;&nbsp;</div>
<img src="{0}"><br/>
""".format(
file_name
)
html = """
<head>
<title>Report</title>
<style>
table {{
width: 100%;
border-collapse: collapse;
}}
td {{
border: 1px solid black;
padding: 2px;
text-align: center;
font-size: 18px;
}}
div,br,p,span {{
white-span: nowrap;
word-warp: break-word;
overflow:hidden;
text-overflow: ellipsis;
}}
img {{
display: block;
margin: 0 auto;
}}
</style>
</head>
<body style='font-family:WenQuanYi Micro Hei;font-size:35px;line-height:22px;font-weight: bold;'>
{}
</body>
""".format(
table_html
)
document.setHtml(html)
printer = QPrinter()
printer.setPageSize(QPagedPaintDevice.Custom)
printer.setPaperSize(QSizeF(30.0, 18.0), QPrinter.Millimeter)
# 设置纸张到条码的边距 左上下右
printer.setPageMargins(0, 5, 0, 0, QPrinter.Millimeter)
document.setPageSize(QSizeF(400.0, 230.0))
print(document.pageSize(), printer.resolution(), printer.pageRect())
print('正在打印中。。。。')
document.print_(printer)
print('打印完成。。')
os.remove(file_name)
if __name__ == '__main__':
kwS = {
"code_number": "200001",
"medicament_name": "1111111",
"purity": "12",
"solvent": "2313",
"start_time": "2022-10-15",
"end_time": "2022-10-16",
"user_name": "测试账户",
}
# CreateBarcode().create_drug_lobel_code(**kwS)
CreateBarcode().create_Code128_img('123456', '硫酸', '研一')
# CreateBarcode().create_Code128_img("200000001")