Search Here

Generate QR Code for QWEB Report and Redirect to URL in Odoo

 This article will teach you how to create and generate QR codes in Odoo using Python for QWEB Custom Reports. After generating the QR code we will redirect to the given URL.

Generate QR Code for QWEB Report and Redirect to URL in Odoo

What is QR code and why do we need this in reporting

It's machine-readable code with a mix of patterns, URLs, and numbers. In Odoo we can use these QR codes in QWEB reporting. This article will provide an insight into how to generate QR codes with URLs, for QWEB reports in the Odoo platform.

Read More: How to Create Custom Report in QWEB

Odoo supports any string in QR code so you can provide any string or number or URL as a pattern to generate the QR code. In this article, I am going to show you two methods to create QR codes for your QWEB Reports.

Read More: How to Add Custom Header and Footer in QWEB Report in Odoo

How to Generate QR Code using Python and Odoo (Method 1)

To generate QR codes using python we need to install the following python packages.

  1. import base64
  2. import qrcode
  3. from io import BytesIO

To install python packages using the below command.

py -m pip install qrcode

Similarly, just change the name of the package and install your desired python module/library. Now create a function inside any Odoo model, I am going to use 'res.company' model.

Read More: How to Link Custom Paper Format in QWEB Report in Odoo

How to put URL inside QR Code in odoo


To put the URL inside the QR code first we need to create a base URL. For this purpose, I am going to use 'ir.config_parameter'. By using this we can make a base URL to redirect some URLs using QR Code.


After creating the URL we will call the above-created function to generate QR Code and pass the base URL to that function. This will return a base64 image string.

base64_qr = self.env['res.company'].generate_qr(base_url)
Now we will use this base64 image string in our QWEB XML code.

<div class="col-xs-6 mt6">
<img t-if="qr" t-att-src="'data:image/png;base64,%s' % base64_qr" style="max-height: 100px;"/>
</div>


How to Generate QR Code using Python and Odoo (Method 2)


In the second method, there is no need to generate a base64 image string using python logic. You just need to create a base URL and pass that URL into the below XML code.


<div>
<img t-att-src="'/report/barcode/?type=%s&amp;value=%s&amp;width=%s&amp;height=%s' % ('QR', 'redirect_url_goes_here', 200, 200)"/>
</div>

Post a Comment

0 Comments