From 3ebe1185a497718a4d2a303d363a1a923dd9effe Mon Sep 17 00:00:00 2001 From: "Tiffany Chang (tic)" Date: Thu, 9 Dec 2021 10:08:04 +0000 Subject: [PATCH] [IMP] base, requirements: support datamatrix barcode printing reportlab package is already in use to print barcodes, but it requires extra packages (pylibdmtx and libdmtx) to print Data Matrix barcodes. Unfortunately the python3-pylibdmtx package is not currently in the Ubuntu version being used by Odoo SaaS and odoo.sh => we cannot install this package automatically. Therefore we leave both packages as optional extra installs for users since pylibdmtx is available via pip3 and additional libdmtx package can be installed at that time (libdmtx0b on linux, other OS info in pylibdmtx pip page). We default to Code128 to avoid blocking stacktrace in case Reportlab cannot print Data Matrices. Part of task: 2494740 Part-of: odoo/odoo#82389 --- odoo/addons/base/models/ir_actions_report.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/odoo/addons/base/models/ir_actions_report.py b/odoo/addons/base/models/ir_actions_report.py index bf4361a1cd0..bfe9681e2c9 100644 --- a/odoo/addons/base/models/ir_actions_report.py +++ b/odoo/addons/base/models/ir_actions_report.py @@ -45,6 +45,12 @@ try: except Exception: pass +datamatrix_available = True +try: + from pylibdmtx import pylibdmtx +except Exception: + _logger.info('A package may be missing to print Data Matrix barcodes: pylibdmtx or libdmtx.') + datamatrix_available = False def _get_wkhtmltopdf_bin(): return find_in_path('wkhtmltopdf') @@ -250,6 +256,16 @@ class IrActionsReport(models.Model): ''' return wkhtmltopdf_state + @api.model + def datamatrix_available(self): + '''Returns whether or not datamatrix creation is possible. + * True: Reportlab seems to be able to create datamatrix without error. + * False: Reportlab cannot seem to create datamatrix, most likely due to missing package dependency + + :return: Boolean + ''' + return datamatrix_available + def get_paperformat(self): return self.paperformat_id or self.env.company.paperformat_id @@ -526,6 +542,9 @@ class IrActionsReport(models.Model): elif barcode_type == 'auto': symbology_guess = {8: 'EAN8', 13: 'EAN13'} barcode_type = symbology_guess.get(len(value), 'Code128') + elif barcode_type == 'DataMatrix' and not self.datamatrix_available(): + # fallback to avoid stacktrack because reportlab won't recognize the type and error message isn't useful/will be blocking + barcode_type = 'Code128' try: width, height, humanreadable, quiet = int(width), int(height), bool(int(humanreadable)), bool(int(quiet)) # for `QR` type, `quiet` is not supported. And is simply ignored.