我可以在报告中打印这样的静态图像:
<img src="data:image/png;base64,${obj.company_id.logo}"/>
我可以用base64代码打印图像:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAlgAAABkCAIAAADVI9l0AAAC/0lEQVR4nO3Vr0vjcRzH8XFF1gw2WRUMFrGvjWFbtdhk2PYnmASTsCSsr1hNwpIwVu1iG9aB/a5+7iP34b7cheNej0f9vD8/vt/y7H3/l2w2m97PttttOTCdTsvV8XhcnbC3t9f7tYuLi07vmc/n5fbBYNCev7m5adz+1f39fbl9tVpVA7vdrtODK+fn5+VpV1dX5erHx0d13Xq97nT+0dFRuf3u7q5cfXl5aX/+YrHodN1kMmmctr+/X80Ph8NyYDablatvb2/VCa+vr53eU7m+vi5PG41G1UC/3y8HlstlubpcLsvVfr9fbR+NRo3PPzw8rObPzs4a8ycnJ+3Pmc1m5fxwOGzPX15elvOTyaQ9X1ksFuX2g4ODTtu/Oj4+Lg+8vb0tV9frdePn9Hq9h4eHcv7p6ak9//j42HjMbrer5lerVWP+/f29fV3l9PS0OmEwGJQD8/m8XH1+fq5O+Pz8/K3f+gvj8bg8bTqdlqvb7ba6brPZ/Ml1f923Tr8bAP4zQghANCEEIJoQAhBNCAGIJoQARBNCAKIJIQDRhBCAaEIIQDQhBCCaEAIQTQgBiCaEAEQTQgCiCSEA0YQQgGhCCEA0IQQgmhACEE0IAYgmhABEE0IAogkhANGEEIBoQghANCEEIJoQAhBNCAGIJoQARBNCAKIJIQDRhBCAaEIIQDQhBCCaEAIQTQgBiCaEAEQTQgCiCSEA0YQQgGhCCEA0IQQgmhACEE0IAYgmhABEE0IAogkhANGEEIBoQghANCEEIJoQAhBNCAGIJoQARBNCAKIJIQDRhBCAaEIIQDQhBCCaEAIQTQgBiCaEAEQTQgCiCSEA0YQQgGhCCEA0IQQgmhACEE0IAYgmhABEE0IAogkhANGEEIBoQghANCEEIJoQAhBNCAGIJoQARBNCAKIJIQDRhBCAaEIIQDQhBCCaEAIQTQgBiCaEAEQTQgCiCSEA0YQQgGhCCEA0IQQgmhACEE0IAYgmhABEE0IAogkhANGEEIBoQghANCEEINoP1gJ3cMlMpaoAAAAASUVORK5CYII=" style="width:300px;height:50px" />
但是如何从网址打印图像?有没有办法在渲染报告时动态地将图像编码为base64?
<img src="img/logo.png" />
实际上我想要做的是在使用mako创建的报告中打印条形码。条形码是使用控制器使用url创建的,因此我需要创建条形码图像,如下所示:
<img src="/report/barcode/?type=EAN13&value=5546903283256&width=600&height=100" style="width:300px;height:50px"/>
我怎样才能做到这一点?
更新:我也试过这个
<img src="/report/barcode/?type=EAN13&value=5546903283256&width=600&height=100" style="width:300px;height:50px"/>
如果我尝试最后两个选项,那么我会收到此错误:
ERROR dnname openerp.service.report: Exception: ('Webkit error', "The command 'wkhtmltopdf' failed with error code = 1. Message: The following diagnosis message was provided:\nExit with code 1 due to network error: ContentNotFoundError\n")
Traceback (most recent call last):
File "/opt/odoo/odoo_pan/src/openerp/service/report.py", line 93, in go
result, format = openerp.report.render_report(cr, uid, ids, object, datas, context)
File "/opt/odoo/odoo_pan/src/openerp/report/__init__.py", line 40, in render_report
return registry['ir.actions.report.xml'].render_report(cr, uid, ids, name, data, context)
File "/opt/odoo/odoo_pan/src/openerp/api.py", line 268, in wrapper
return old_api(self, *args, **kwargs)
File "/opt/odoo/odoo_pan/src/addons/base/ir/ir_actions.py", line 156, in render_report
return new_report.create(cr, uid, res_ids, data, context)
File "/opt/odoo/odoo_pan/src/addons/report_webkit/webkit_report.py", line 377, in create
result = self.create_source_pdf(cursor, uid, ids, data, report_xml, context)
File "/opt/odoo/odoo_pan/src/openerp/report/report_sxw.py", line 462, in create_source_pdf
return self.create_single_pdf(cr, uid, ids, data, report_xml, context)
File "/opt/odoo/odoo_pan/src/addons/report_webkit/webkit_report.py", line 358, in create_single_pdf
pdf = self.generate_pdf(bin, report_xml, head, foot, htmls)
File "/opt/odoo/odoo_pan/src/addons/report_webkit/webkit_report.py", line 220, in generate_pdf
_("The command 'wkhtmltopdf' failed with error code = %s. Message: %s") % (status, error_message))
except_orm: ('Webkit error', "The command 'wkhtmltopdf' failed with error code = 1. Message: The following diagnosis message was provided:\nExit with code 1 due to network error: ContentNotFoundError\n")
答案 0 :(得分:0)
这可能只是格式化错误。尝试改变“&amp; amp;”到'&amp;':
<img src="/report/barcode/?type=EAN13&value=5546903283256&width=600&height=100" style="width:300px;height:50px;"/>
多个GET参数由'&amp;'分隔。
顺便说一下:为什么不使用这些参数来获得合适的图像尺寸,而不需要在之后设置样式:
<img src="/report/barcode/?type=EAN13&value=5546903283256&width=300&height=50" />
答案 1 :(得分:0)
最后,我创建了一个新的计算字段以获取base64代码
class StockMove(models.Model):
_inherit = "stock.move"
barcode_image = fields.Text(
string='Barcode image',
compute='_compute_barcode_image',
)
@api.one
@api.depends('product_id')
def _compute_barcode_image(self):
if self.product_id.product_tmpl_id.ean13:
try:
barcode = self.env['report'].barcode(
'EAN13',
self.product_id.product_tmpl_id.ean13,
width=300,
height=50,
humanreadable=0
)
except (ValueError, AttributeError):
raise Warning(_('Cannot convert into barcode.'))
barcode_base64 = base64.b64encode(barcode)
self.barcode_image = 'data:image/png;base64,' + barcode_base64
else:
self.barcode_image = None
我将此添加到mako报告中:
%if line.product_id and line.product_id.ean13:
<span>
<img src="${line.barcode_image}"
style="width:300px; height:50px" />
</span>
%endif