Search code examples
pythonodooodoo-13

How to show company logo in report generated through python code in odoo


I have a report I want to customize to add the company logo to the report, the problem is the whole report is generated by code. I want to print the company logo on the report, I can print the company name which indicates its working but with the logo, it seems due to path issues it does not seem to work. Here is my code with a snippet of what I have done so far.

def get_pdf(self, options, minimal_layout=True):
        if not config['test_enable']:
            self = self.with_context(commit_assetsbundle=True)

        base_url = self.env['ir.config_parameter'].sudo().get_param('report.url') or self.env['ir.config_parameter'].sudo().get_param('web.base.url')
        rcontext = {
            'mode': 'print',
            'base_url': base_url,
            'company': self.env.company,
        }

        body = self.env['ir.ui.view'].render_template(
            "account_reports.print_template",
            values=dict(rcontext),
        )
        body_html = self.with_context(print_mode=True).get_html(options)
        logging.info(self.env.user.company_id.name)
        logging.info(self.env.user.company_id.logo)
        test = "<header>"\
               "<img style='width: 7.0cm; height: 1.8cm;' src='/web/binary/{}' alt='logo'/>"\
               "</header>".format(self.env.user.company_id.logo)
        ttt = bytes(test, 'utf-8')

Like I said it displays the company's name but not the logo even though the logo is set in the system.


Solution

  • So I was able to fix it, I remembered that odoo has an image_data_url function that it uses to get the path to an image stored in the database and display in qweb report template, so I checked and found the function in odoo.tools and I called the function in my model passing the company logo as an argument to the function and calling it in the image src attribute, so it looked like this

    from odoo import tools
    test = "<header style='margin-left: 750px;'>"\
                   "<img style='width: 7.0cm; height: 1.8cm;'" \
                   "src='{}' alt='logo'/>"\
                   "</header>". \
                   format(tools.image_data_uri(self.env.user.company_id.logo))
            ttt = bytes(test, 'utf-8')```