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.
yy_rms_39zhiyao_duizhao/Common/create_barcode.py

152 lines
4.9 KiB

2 years ago
"""
此文件是用来生成Code128条形码 并实现打印功能
"""
import os
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import QApplication
from PyQt5.QtPrintSupport import QPrinter
from pystrich.code128 import Code128Encoder
import uuid
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)
2 years ago
os.makedirs(code_path)
def __init__(self):
pass
def create_Code128_img(self, barcode):
a = Code128Encoder(barcode, options={
'ttf_font': os.path.join(os.getcwd(),'arial.ttf'),
'label_border': 0, 'height': 45, 'bottom_border': 0,'ttf_fontsize':13,
})
# bar_width 条码宽度尺寸
file_name = os.path.join(self.code_path, str(uuid.uuid4()) + '.png')
a.save(file_name, bar_width=1)
self.printer_code(file_name)
def printer_code(self, file_name):
a = QApplication([])
document = QTextDocument()
html = """
<head>
<title>Report</title>
<style>
</style>
</head>
<body>
<img src="{}">
</body>
""".format(file_name)
document.setHtml(html)
printer = QPrinter()
printer.setPageSize(QPagedPaintDevice.Custom)
printer.setPaperSize(QSizeF(30.0,18.0),QPrinter.Millimeter)
# 设置纸张到条码的边距 左上下右
printer.setPageMargins(0, 3, 0, 0, QPrinter.Millimeter)
#document.setPageSize(QSizeF(printer.pageRect().size()))
document.setPageSize(QSizeF(100.0,65.0))
print('正在打印中。。。。')
document.print_(printer)
print('打印完成。。')
# os.remove(file_name)
def create_drug_lobel_code(self, **kwargs):
a = Code128Encoder(
# kwargs.get("code_number"),
"111",
2 years ago
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)
2 years ago
a.save(file_name, bar_width=1)
kwargs["file_path"] = file_name
self.printer_drug_label(**kwargs)
def printer_drug_label(self, **kwargs):
a = QApplication([])
document = QTextDocument()
# <p style="font-size:12px;">
2 years ago
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;display:flex;text-align:center !important;justify-content:center;width: 100%;">华润三九制药</span> <br />
试剂名称: {} <br />
级别: {} <br />
批号: {} <br />
2 years ago
编号: {} <br />
货位号: {} <br />
2 years ago
</p>
<img src="{}">
</div>
</body>
""".format(
kwargs.get("name"),
kwargs.get("purity"),
kwargs.get("standard_code"),
kwargs.get("remark12"),
kwargs.get("cell_position_code"),
2 years ago
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)
2 years ago
# printer.setPaperSize(QSizeF(30.0,50.0),QPrinter.Millimeter)
# 设置纸张到条码的边距 左上下右
# printer.setPageMargins(6, 2, 0, 0, QPrinter.Millimeter)
printer.setPageMargins(20, 20, 0, 0, QPrinter.Millimeter)
2 years ago
document.setPageSize(QSizeF(printer.pageRect().size()))
#document.setPageSize(QSizeF(50.0,30.0))
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("200000001")