通过Odoo 12(CE)中的Pizzle 6.0.0合并.pdf中的.png文件时出现问题
在Odoo 12 CE的自定义模块中,我扩展了sale.order.line类并添加了将单独的.png图像合并到一个.pdf文件中的函数。对于此任务,我使用library Pillow 6.0.0通过Odoo 12(CE)中的Pizzle 6.0.0合并.pdf中的.png文件时出现问题,odoo,odoo-12,Odoo,Odoo 12,在Odoo 12 CE的自定义模块中,我扩展了sale.order.line类并添加了将单独的.png图像合并到一个.pdf文件中的函数。对于此任务,我使用library Pillow 6.0.0 image_files = [<PIL.Image.Image image mode=RGB size=800x1400 at 0x118295908>, <PIL.Image.Image image mode=RGB size=800x1400 at 0x118295B38>
image_files = [<PIL.Image.Image image mode=RGB size=800x1400 at 0x118295908>, <PIL.Image.Image image mode=RGB size=800x1400 at 0x118295B38>]
filename = "test.pdf"
image_files[0].save(filename, "PDF", resolution=100.0, save_all=True, append_images=image_files[1:])
疑难解答:我在Odoo之外创建了test.py项目,在那里,我使用相同的虚拟环境和相同的软件包测试了该功能,它按预期工作:所有png文件都合并到一个pdf文件中
当我从Odoo模块运行它时,为什么它不工作?我不知道用它做枕头,但我使用了FPDF,它应该转换为png到pdf
import os
from odoo.tools import pdf
from fpdf import FPDF
import base64
from odoo import models, fields, api, _
from odoo.exceptions import Warning, ValidationError
@api.multi
def download_labels(self):
self.ensure_one()
try:
file_path = "/tmp/waves/"
directory = os.path.dirname(file_path)
os.stat(directory)
os.system("mkdir %s" % (file_path))
# "P" indiacte Portal page for pdf , mm indicate
pdf_info = FPDF('P', 'mm', (297, 210))
pdf_datas = []
for line in self.order_line:
# Create Directory -
file_name = line.name
file_name = file_name.replace('/', '_')
attachment_id = self.env['ir.attachment'].search([('res_id', '=', line.id), ('res_model', '=', line._name)], limit=1)
file_extension = attachment_id.name.split('.')[1] if attachment_id.name.split('.')[1] else "pdf"
if attachment_id:
if file_extension in ['pdf', 'PDF']:
pdf_datas.append(base64.decodestring(attachment_id.datas))
continue
with open("%s%s.%s" % (file_path, file_name, file_extension), "wb") as f:
f.write(base64.decodestring(attachment_id.datas))
pdf_info.add_page()
pdf_info.image("%s%s.%s" % (file_path, file_name, file_extension), 1, 1, 296, 209)
if pdf_datas:
message_ept = (_("All shipment label combined!"))
message = self.message_post(body=message_ept, attachments=[('Label-%s.%s' % (self.id, 'PDF'), pdf.merge_pdf(pdf_datas))])
return {
'type': 'ir.actions.act_url',
'url': '/web/binary/download_document?model=ir.attachment&field=datas&id=%s&filename=%s.pdf' % ( message.attachment_ids[0].id, self.name.replace('/', '_')),
'target': 'self',
}
pdf_info.output("%s%s.pdf" % (file_path, self.name.replace('/', '_')), "F")
binary_package = open("%s%s.pdf" % (file_path, self.name.replace('/', '_')), 'rb').read()
os.system("rm -R %s" % (directory))
message_ept = (_("label combined!"))
message = self.message_post(body=message_ept, attachments=[("%s.pdf" % (self.name.replace('/', '_')), binary_package)])
return {
'type': 'ir.actions.act_url',
'url': '/web/binary/download_document?model=ir.attachment&field=datas&id=%s&filename=%s.pdf' % ( message.attachment_ids[0].id, self.name.replace('/', '_')),
'target': 'self',
}
except Exception as e:
raise ValidationError(e)
我可能会迟到,但这里有解决办法。尝试在逻辑之前输入这些
Image._initialized = 0
Image.init()
嗨,哈希特,谢谢你的回答。我还重写了我的代码,并使用FPDF作为解决方法,但我仍然想理解为什么这段代码在Odoo中不起作用,而在python项目中它工作得很好。@edward你能告诉我你的问题的更具体的情况吗,或者放上完整的堆栈跟踪。我在git上添加了完整的测试函数,因此,您可以复制它并尝试从您的odoo环境中运行,以了解问题:
Image._initialized = 0
Image.init()