From 9918f75e8936002e1b09c0bf593e90ba812b8beb Mon Sep 17 00:00:00 2001 From: Ben Hagen Date: Thu, 8 May 2025 11:33:08 +0200 Subject: [PATCH] Add configurable page margins to label sheet plugin (#9641) Co-authored-by: Oliver --- .../plugin/builtin/labels/label_sheet.py | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/backend/InvenTree/plugin/builtin/labels/label_sheet.py b/src/backend/InvenTree/plugin/builtin/labels/label_sheet.py index 40f9d9639f..6b4afe7760 100644 --- a/src/backend/InvenTree/plugin/builtin/labels/label_sheet.py +++ b/src/backend/InvenTree/plugin/builtin/labels/label_sheet.py @@ -49,6 +49,13 @@ class LabelPrintingOptionsSerializer(serializers.Serializer): help_text=_('Print the label sheet in landscape mode'), ) + margin = serializers.IntegerField( + default=10, + label=_('Page Margin'), + help_text=_('Margin around the page in mm'), + min_value=0, + ) + class InvenTreeLabelSheetPlugin(LabelPrintingMixin, SettingsMixin, InvenTreePlugin): """Builtin plugin for label printing. @@ -60,7 +67,7 @@ class InvenTreeLabelSheetPlugin(LabelPrintingMixin, SettingsMixin, InvenTreePlug NAME = 'InvenTreeLabelSheet' TITLE = _('InvenTree Label Sheet Printer') DESCRIPTION = _('Arrays multiple labels onto a single sheet') - VERSION = '1.0.0' + VERSION = '1.0.1' AUTHOR = _('InvenTree contributors') BLOCKING_PRINT = True @@ -91,6 +98,9 @@ class InvenTreeLabelSheetPlugin(LabelPrintingMixin, SettingsMixin, InvenTreePlug border = printing_options.get('border', False) skip = int(printing_options.get('skip', 0)) + # Get margin from printing options + margin = printing_options.get('margin', 10) + # Extract size of page page_size = report.helpers.page_size(page_size_code) page_width, page_height = page_size @@ -98,9 +108,13 @@ class InvenTreeLabelSheetPlugin(LabelPrintingMixin, SettingsMixin, InvenTreePlug if landscape: page_width, page_height = page_height, page_width - # Calculate number of rows and columns - n_cols = math.floor(page_width / label.width) - n_rows = math.floor(page_height / label.height) + # Calculate available space after margins + available_width = page_width - (2 * margin) + available_height = page_height - (2 * margin) + + # Calculate number of rows and columns based on available space + n_cols = math.floor(available_width / label.width) + n_rows = math.floor(available_height / label.height) n_cells = n_cols * n_rows if n_cells == 0: @@ -123,6 +137,7 @@ class InvenTreeLabelSheetPlugin(LabelPrintingMixin, SettingsMixin, InvenTreePlug 'n_pages': math.ceil(n_labels / n_cells), 'n_cols': n_cols, 'n_rows': n_rows, + 'margin': margin, } pages = [] @@ -232,6 +247,8 @@ class InvenTreeLabelSheetPlugin(LabelPrintingMixin, SettingsMixin, InvenTreePlug n_rows = kwargs['n_rows'] n_cols = kwargs['n_cols'] + margin = kwargs.get('margin', 0) + inner = ''.join(pages) # Generate styles for individual cells (on each page) @@ -262,7 +279,7 @@ class InvenTreeLabelSheetPlugin(LabelPrintingMixin, SettingsMixin, InvenTreePlug