Search code examples
imagebase64odoo-8odoomako

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


I can print a static image like this in reports:

<img src="data:image/png;base64,${obj.company_id.logo}"/>

And I can print images in base64 code like this:

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

But how can I print images from a url? Is there a way to encode the image to base64 dynamically while rendering the report?

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

Actually what I want to do is to print barcodes in a report created with mako. The barcodes are created with a controller using a url, so I need to create the barcode image like this:

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

How can I achieve this?

Update: I also tried this

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

And if I try both last options then I get this error:

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

Solution

  • Finally I created a new computed field in order to get the base64 code

    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
    

    And I added this to the mako report:

    %if line.product_id and line.product_id.ean13:
        <span>
            <img src="${line.barcode_image}" 
                 style="width:300px; height:50px" />
        </span>
    %endif