如何在 Odoo/OpenERP 的 mako 报告中打印条形码?

How to print barcodes in mako reports in Odoo/OpenERP?

我可以在报告中打印这样的静态图像:

<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" /> 

但是如何从 url 打印图像?有没有办法在渲染报表的同时将图像动态编码为base64?

<img src="img/logo.png" />

实际上我想做的是在用 mako 创建的报告中打印条形码。条形码是通过使用 url 的控制器创建的,因此我需要像这样创建条形码图像:

<img src="/report/barcode/?type=EAN13&amp;value=5546903283256&amp;width=600&amp;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")

这可能只是格式错误。尝试改变“&”到 '&':

<img src="/report/barcode/?type=EAN13&value=5546903283256&width=600&height=100" style="width:300px;height:50px;"/>

多个GET参数用'&'分隔。

顺便说一下:为什么不使用参数来获得正确尺寸的图像而不需要事后对其进行样式设置:

<img src="/report/barcode/?type=EAN13&value=5546903283256&width=300&height=50" />

最后我创建了一个新的计算字段以获取 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