OCA reporting engine fork for dev and update.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.3 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2019 Creu Blanca
  3. # Copyright 2019 Tecnativa - David Vidal
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. from odoo import api, models
  6. import qrcode
  7. from qrcode.image import svg, pil
  8. import io
  9. class Report(models.Model):
  10. _inherit = 'report'
  11. @api.model
  12. def qr_generate(self, value, box_size=3, border=5,
  13. factory='png', **kwargs):
  14. factories = {
  15. 'png': pil.PilImage,
  16. 'svg': svg.SvgImage,
  17. 'svg-fragment': svg.SvgFragmentImage,
  18. 'svg-path': svg.SvgPathImage,
  19. }
  20. # Color parameters seem to be inverted in the library
  21. back_color = kwargs.pop("back_color", "black")
  22. fill_color = kwargs.pop("fill_color", "white")
  23. try:
  24. # Defaults to png if the argument is unknown
  25. image_factory = factories.get(factory, pil.PilImage)
  26. qr = qrcode.QRCode(
  27. box_size=box_size, border=border,
  28. image_factory=image_factory, **kwargs)
  29. qr.add_data(value)
  30. qr.make()
  31. img = qr.make_image(fill_color=fill_color, back_color=back_color)
  32. arr = io.BytesIO()
  33. img.save(arr)
  34. return arr.getvalue()
  35. except Exception:
  36. raise ValueError("Cannot convert into barcode.")