编程单增加零件图号
This commit is contained in:
@@ -103,12 +103,19 @@ class PrintingUtils(models.AbstractModel):
|
|||||||
self.send_to_printer(host, port, zpl_code)
|
self.send_to_printer(host, port, zpl_code)
|
||||||
|
|
||||||
|
|
||||||
def add_qr_code_to_pdf(self, pdf_path:str, content:str, buttom_text:Optional[str]=False):
|
def add_qr_code_to_pdf(
|
||||||
|
self,
|
||||||
|
pdf_path:str,
|
||||||
|
content:str,
|
||||||
|
qr_code_buttom_text:Optional[str]=False,
|
||||||
|
buttom_text:Optional[str]=False,
|
||||||
|
):
|
||||||
"""
|
"""
|
||||||
在PDF文件中添加二维码
|
在PDF文件中添加二维码
|
||||||
:param pdf_path: PDF文件路径
|
:param pdf_path: PDF文件路径
|
||||||
:param content: 二维码内容
|
:param content: 二维码内容
|
||||||
:param buttom_text: 二维码下方文字
|
:param qr_code_buttom_text: 二维码下方文字
|
||||||
|
:param buttom_text: 正文下方文字
|
||||||
:return: 是否成功
|
:return: 是否成功
|
||||||
"""
|
"""
|
||||||
if not os.path.exists(pdf_path):
|
if not os.path.exists(pdf_path):
|
||||||
@@ -156,8 +163,9 @@ class PrintingUtils(models.AbstractModel):
|
|||||||
existing_pdf = PdfFileReader(original_file)
|
existing_pdf = PdfFileReader(original_file)
|
||||||
output = PdfFileWriter()
|
output = PdfFileWriter()
|
||||||
|
|
||||||
# 处理第一页
|
# 处理最后一页
|
||||||
page = existing_pdf.getPage(0)
|
last_page = existing_pdf.getNumPages() - 1
|
||||||
|
page = existing_pdf.getPage(last_page)
|
||||||
# 获取页面尺寸
|
# 获取页面尺寸
|
||||||
page_width = float(page.mediaBox.getWidth())
|
page_width = float(page.mediaBox.getWidth())
|
||||||
page_height = float(page.mediaBox.getHeight())
|
page_height = float(page.mediaBox.getHeight())
|
||||||
@@ -179,14 +187,22 @@ class PrintingUtils(models.AbstractModel):
|
|||||||
qr_y = margin + 20 # 将二维码向上移动一点,为文字留出空间
|
qr_y = margin + 20 # 将二维码向上移动一点,为文字留出空间
|
||||||
c.drawImage(qr_temp_path, page_width - qr_size - margin, qr_y, width=qr_size, height=qr_size)
|
c.drawImage(qr_temp_path, page_width - qr_size - margin, qr_y, width=qr_size, height=qr_size)
|
||||||
|
|
||||||
if buttom_text:
|
if qr_code_buttom_text:
|
||||||
# 在二维码下方绘制文字
|
# 在二维码下方绘制文字
|
||||||
text = buttom_text
|
text = qr_code_buttom_text
|
||||||
text_width = c.stringWidth(text, "SimSun" if font_found else "Helvetica", 10) # 准确计算文字宽度
|
text_width = c.stringWidth(text, "SimSun" if font_found else "Helvetica", 10) # 准确计算文字宽度
|
||||||
text_x = page_width - qr_size - margin + (qr_size - text_width) / 2 # 文字居中对齐
|
text_x = page_width - qr_size - margin + (qr_size - text_width) / 2 # 文字居中对齐
|
||||||
text_y = margin + 20 # 文字位置靠近底部
|
text_y = margin + 20 # 文字位置靠近底部
|
||||||
c.drawString(text_x, text_y, text)
|
c.drawString(text_x, text_y, text)
|
||||||
|
|
||||||
|
if buttom_text:
|
||||||
|
# 在下方中间添加文字
|
||||||
|
text = button_text
|
||||||
|
text_width = c.stringWidth(text, "SimSun" if font_found else "Helvetica", 10) # 准确计算文字宽度
|
||||||
|
text_x = (page_width - text_width) / 2 # 文字居中对齐
|
||||||
|
text_y = margin + 20 # 文字位置靠近底部
|
||||||
|
c.drawString(text_x, text_y, text)
|
||||||
|
|
||||||
c.save()
|
c.save()
|
||||||
|
|
||||||
# 读取带有二维码的临时PDF
|
# 读取带有二维码的临时PDF
|
||||||
@@ -196,12 +212,13 @@ class PrintingUtils(models.AbstractModel):
|
|||||||
|
|
||||||
# 合并原始页面和二维码页面
|
# 合并原始页面和二维码页面
|
||||||
page.mergePage(qr_page)
|
page.mergePage(qr_page)
|
||||||
output.addPage(page)
|
|
||||||
|
|
||||||
# 添加剩余的页面
|
# 添加剩余的页面
|
||||||
for i in range(1, existing_pdf.getNumPages()):
|
for i in range(0, last_page):
|
||||||
output.addPage(existing_pdf.getPage(i))
|
output.addPage(existing_pdf.getPage(i))
|
||||||
|
|
||||||
|
output.addPage(page)
|
||||||
|
|
||||||
# 保存最终的PDF到一个临时文件
|
# 保存最终的PDF到一个临时文件
|
||||||
final_temp_path = pdf_path + '.tmp'
|
final_temp_path = pdf_path + '.tmp'
|
||||||
with open(final_temp_path, "wb") as output_file:
|
with open(final_temp_path, "wb") as output_file:
|
||||||
|
|||||||
Reference in New Issue
Block a user