Odoo使用Sign模块生成图文PDF - xiaohao0576/odoo-doc GitHub Wiki

以下代码虽然繁琐,不够优雅,但是可以正确运行

images = env['ir.attachment'].search([("res_model", "=", "fleet.vehicle.log.services"), ("mimetype", "ilike", "image"),("res_id", "=", record.id)])

if not images:
    raise UserError("没有找到图片附件!")

if len(images) <=3:
    template = env['sign.template'].browse(3) # 3张图片的PDF模板文件
    partner_name = env['sign.item'].browse(8) # 车主姓名字段
    vehicle_number = env['sign.item'].browse(7) # 车牌号码
    problem_level =  env['sign.item'].browse(9) # 问题级别
    image0 = env['sign.item'].browse(10)
    image1 = env['sign.item'].browse(12)
    image2 = env['sign.item'].browse(13)

if len(images) > 3:
    template = env['sign.template'].browse(4) # 6张图片的PDF模板文件
    partner_name = env['sign.item'].browse(14) # 车主姓名字段
    vehicle_number = env['sign.item'].browse(15) # 车牌号码
    problem_level =  env['sign.item'].browse(16) # 问题级别
    image0 = env['sign.item'].browse(23)
    image1 = env['sign.item'].browse(25)
    image2 = env['sign.item'].browse(28)
    image3 = env['sign.item'].browse(26)
    image4 = env['sign.item'].browse(27)
    image5 = env['sign.item'].browse(29)
    

if not template:
    raise UserError("找不到PDF模板文件!")

role = template.sign_item_ids.responsible_id
sign_items = template.sign_item_ids

sign_request = env['sign.request'].with_context(no_sign_mail=True).create({
    'template_id': template.id,
    'request_item_ids': [Command.create({'role_id': role.id, 'partner_id': env.user.partner_id.id})],
    'reference': f'车辆服务单-{record.id}',  #这个是生成的文件名
    'state': 'sent',
})

request_item = sign_request.request_item_ids[0].sudo() # 需要用超级权限才能模拟签名


item_values = []  #填空列表

# 添加车主姓名
item_values.append(Command.create({
        'sign_item_id':partner_name.id,
        'value': '张三'
    }))

# 添加车牌号码
item_values.append(Command.create({
        'sign_item_id':vehicle_number.id,
        'value': '2BC-3721'
    }))

# 添加问题级别
item_values.append(Command.create({
        'sign_item_id':problem_level.id,
        'value': '中-需要协调处理'
    }))

# 添加图片
for image in images:
    image._compute_datas()
    
    
try:
    # 添加图片1
    item_values.append(Command.create({
        'sign_item_id':image0.id,
        'value': f"data:{images[0].mimetype};base64,{images[0].datas.decode('utf-8')}"
    }))
    
    # 添加图片2
    item_values.append(Command.create({
        'sign_item_id':image1.id,
        'value': f"data:{images[1].mimetype};base64,{images[1].datas.decode('utf-8')}"
    }))
    
    # 添加图片3
    item_values.append(Command.create({
        'sign_item_id':image2.id,
        'value': f"data:{images[2].mimetype};base64,{images[2].datas.decode('utf-8')}"
    }))

    # 添加图片4
    item_values.append(Command.create({
        'sign_item_id':image3.id,
        'value': f"data:{images[3].mimetype};base64,{images[3].datas.decode('utf-8')}"
    }))
    
    # 添加图片5
    item_values.append(Command.create({
        'sign_item_id':image4.id,
        'value': f"data:{images[4].mimetype};base64,{images[4].datas.decode('utf-8')}"
    }))
    
    # 添加图片6
    item_values.append(Command.create({
        'sign_item_id':image5.id,
        'value': f"data:{images[5].mimetype};base64,{images[5].datas.decode('utf-8')}"
    }))
    
except:
    pass


request_item['sign_item_value_ids'] = item_values
request_item['state'] = 'completed'
sign_request['state'] = 'signed'

env["ir.config_parameter"].sudo().set_param("sign.use_custom_font", "Sun-ExtA") # 设置新字体,可生成中文

action = sign_request.get_completed_document()