From 75c3ae6fd17dfcda6c00a029231ca2ae254477cc Mon Sep 17 00:00:00 2001 From: Jos De Graeve Date: Thu, 2 Nov 2017 10:04:25 +0100 Subject: [PATCH] Added graphic image support --- printer_zpl2/README.rst | 1 + printer_zpl2/__manifest__.py | 2 +- printer_zpl2/models/printing_label_zpl2.py | 26 +++++++++++++++++++ .../models/printing_label_zpl2_component.py | 5 ++++ printer_zpl2/views/printing_label_zpl2.xml | 5 ++-- 5 files changed, 36 insertions(+), 3 deletions(-) diff --git a/printer_zpl2/README.rst b/printer_zpl2/README.rst index 271aec8..cded79e 100644 --- a/printer_zpl2/README.rst +++ b/printer_zpl2/README.rst @@ -82,6 +82,7 @@ Contributors ------------ * Sylvain Garancher +* Jos De Graeve Maintainer ---------- diff --git a/printer_zpl2/__manifest__.py b/printer_zpl2/__manifest__.py index 92b81d1..65fb68d 100644 --- a/printer_zpl2/__manifest__.py +++ b/printer_zpl2/__manifest__.py @@ -6,7 +6,7 @@ 'name': 'Printer ZPL II', 'version': '10.0.1.1.1', 'category': 'Printer', - 'author': 'SYLEAM, Odoo Community Association (OCA)', + 'author': 'SYLEAM, Apertoso NV, Odoo Community Association (OCA)', 'website': 'http://www.syleam.fr/', 'license': 'AGPL-3', 'external_dependencies': { diff --git a/printer_zpl2/models/printing_label_zpl2.py b/printer_zpl2/models/printing_label_zpl2.py index 760c72f..9a5fbfc 100644 --- a/printer_zpl2/models/printing_label_zpl2.py +++ b/printer_zpl2/models/printing_label_zpl2.py @@ -3,8 +3,11 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). import time +import base64 import datetime +import io import logging +from PIL import Image, ImageOps from odoo import api, exceptions, fields, models from odoo.tools.translate import _ from odoo.tools.safe_eval import safe_eval @@ -117,6 +120,29 @@ class PrintingLabelZpl2(models.Model): zpl2.ARG_COLOR: component.color, zpl2.ARG_ROUNDING: component.rounding, }) + elif component.component_type == 'graphic': + pil_image = Image.open(io.BytesIO( + base64.b64decode(component.graphic_image or data))) + if component.width and component.height: + pil_image = pil_image.resize( + (component.width, component.height)) + + # Invert the colors + if component.reverse_print: + pil_image = ImageOps.invert(pil_image) + + # Rotation (PIL rotates counter clockwise) + if component.orientation == zpl2.ORIENTATION_ROTATED: + pil_image = pil_image.transpose(Image.ROTATE_270) + elif component.orientation == zpl2.ORIENTATION_INVERTED: + pil_image = pil_image.transpose(Image.ROTATE_180) + elif component.orientation == zpl2.ORIENTATION_BOTTOM_UP: + pil_image = pil_image.transpose(Image.ROTATE_90) + + label_data.graphic_field( + component_offset_x, component_offset_y, + pil_image + ) elif component.component_type == 'circle': label_data.graphic_circle( component_offset_x, component_offset_y, { diff --git a/printer_zpl2/models/printing_label_zpl2_component.py b/printer_zpl2/models/printing_label_zpl2_component.py index c62cad6..35b4f0c 100644 --- a/printer_zpl2/models/printing_label_zpl2_component.py +++ b/printer_zpl2/models/printing_label_zpl2_component.py @@ -34,6 +34,7 @@ class PrintingLabelZpl2Component(models.Model): ('text', 'Text'), ('rectangle', 'Rectangle / Line'), ('circle', 'Circle'), + ('graphic', 'Graphic'), (zpl2.BARCODE_CODE_11, 'Code 11'), (zpl2.BARCODE_INTERLEAVED_2_OF_5, 'Interleaved 2 of 5'), (zpl2.BARCODE_CODE_39, 'Code 39'), @@ -142,3 +143,7 @@ class PrintingLabelZpl2Component(models.Model): block_left_margin = fields.Integer( string='Left Margin', help='Left margin for the second and other lines in the block.') + graphic_image = fields.Binary( + string='Image', attachment=True, + help='This field holds a static image to print. ' + 'If not set, the data field is evaluated.') diff --git a/printer_zpl2/views/printing_label_zpl2.xml b/printer_zpl2/views/printing_label_zpl2.xml index 2782d8f..3b32230 100644 --- a/printer_zpl2/views/printing_label_zpl2.xml +++ b/printer_zpl2/views/printing_label_zpl2.xml @@ -52,6 +52,7 @@ + @@ -59,7 +60,7 @@ - + @@ -69,7 +70,7 @@ - +