Accept Merge Request #2071: (feature/6694 -> develop)

Merge Request: 解决pdf上数字乱码的问题

Created By: @胡尧
Accepted By: @胡尧
URL: https://jikimo-hn.coding.net/p/jikimo_sfs/d/jikimo_sf/git/merge/2071?initial=true
This commit is contained in:
胡尧
2025-04-27 14:13:45 +08:00
committed by Coding
2 changed files with 24 additions and 11 deletions

View File

@@ -9,7 +9,8 @@ class MrpProduction(models.Model):
@api.depends('state') @api.depends('state')
def _compute_pr_mp_count(self): def _compute_pr_mp_count(self):
for item in self: for item in self:
pr_ids = self.env['purchase.request'].sudo().search([('origin', 'like', item.name), ('is_subcontract', '!=', 'True')]) pr_ids = self.env['purchase.request'].sudo().search([('origin', 'like', item.name)])
# pr_ids = self.env['purchase.request'].sudo().search([('origin', 'like', item.name), ('is_subcontract', '!=', 'True')])
if pr_ids: if pr_ids:
item.pr_mp_count = len(pr_ids) item.pr_mp_count = len(pr_ids)
else: else:

View File

@@ -129,23 +129,23 @@ class PrintingUtils(models.AbstractModel):
output_temp_path = '/tmp/output_temp.pdf' output_temp_path = '/tmp/output_temp.pdf'
try: try:
# 使用reportlab创建一个新的PDF
# 注册中文字体 # 注册中文字体
font_paths = [ font_paths = [
"c:/windows/fonts/simsun.ttc", # Windows系统宋体
"/usr/share/fonts/windows/simsun.ttc", # Windows系统宋体 "/usr/share/fonts/windows/simsun.ttc", # Windows系统宋体
"c:/windows/fonts/simsun.ttc", # Windows系统宋体另一个位置
"/usr/share/fonts/truetype/droid/DroidSansFallbackFull.ttf", # Linux Droid字体 "/usr/share/fonts/truetype/droid/DroidSansFallbackFull.ttf", # Linux Droid字体
"/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc", # 文泉驿正黑 "/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc", # 文泉驿正黑
"/usr/share/fonts/chinese/TrueType/simsun.ttc", # 某些Linux发行版位置 "/usr/share/fonts/chinese/TrueType/simsun.ttc", # 某些Linux发行版位置
"/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc", # Noto Sans CJK
] ]
font_found = False font_found = False
for font_path in font_paths: for font_path in font_paths:
if os.path.exists(font_path): if os.path.exists(font_path):
try: try:
# 注册两个字体,一个用于中文,一个用于数字
pdfmetrics.registerFont(TTFont('SimSun', font_path)) pdfmetrics.registerFont(TTFont('SimSun', font_path))
pdfmetrics.registerFont(TTFont('Arial', 'c:/windows/fonts/arial.ttf'))
font_found = True font_found = True
break break
except: except:
@@ -167,7 +167,7 @@ class PrintingUtils(models.AbstractModel):
# 设置字体 # 设置字体
if font_found: if font_found:
c.setFont('SimSun', 10) # 增大字体大小到14pt c.setFont('SimSun', 10) # 用于中文文本
else: else:
# 如果没有找到中文字体,使用默认字体 # 如果没有找到中文字体,使用默认字体
c.setFont('Helvetica', 10) c.setFont('Helvetica', 10)
@@ -181,11 +181,23 @@ class PrintingUtils(models.AbstractModel):
if buttom_text: if buttom_text:
# 在二维码下方绘制文字 # 在二维码下方绘制文字
text = buttom_text # 分离中文和数字
text_width = c.stringWidth(text, "SimSun" if font_found else "Helvetica", 10) # 准确计算文字宽度 import re
text_x = page_width - qr_size - margin + (qr_size - text_width) / 2 # 文字居中对齐 text_parts = re.split('(\d+)', buttom_text)
text_y = margin + 20 # 文字位置靠近底部 text_x = page_width - qr_size - margin + (qr_size - c.stringWidth(buttom_text, "SimSun", 10)) / 2
c.drawString(text_x, text_y, text) text_y = margin + 20
current_x = text_x
for part in text_parts:
if part: # 跳过空字符串
if part.isdigit():
c.setFont('Arial', 10) # 数字使用Arial字体
current_font = 'Arial'
else:
c.setFont('SimSun', 10) # 中文使用宋体
current_font = 'SimSun'
c.drawString(current_x, text_y, part)
current_x += c.stringWidth(part, current_font, 10)
c.save() c.save()