From 4e6879407e61b0e0015435d504f156807bd0ee2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikl=C3=B3s=20M=C3=A1rton?= Date: Thu, 11 Jul 2024 05:28:29 +0200 Subject: [PATCH] Add debug option to the sheet printer plugin (#7614) --- .../plugin/builtin/labels/label_sheet.py | 28 +++++++++++++++---- 1 file changed, 23 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 bf88e0f132..9bee021f00 100644 --- a/src/backend/InvenTree/plugin/builtin/labels/label_sheet.py +++ b/src/backend/InvenTree/plugin/builtin/labels/label_sheet.py @@ -12,6 +12,7 @@ import weasyprint from rest_framework import serializers import report.helpers +from InvenTree.helpers import str2bool from plugin import InvenTreePlugin from plugin.mixins import LabelPrintingMixin, SettingsMixin from report.models import LabelOutput, LabelTemplate @@ -64,9 +65,22 @@ class InvenTreeLabelSheetPlugin(LabelPrintingMixin, SettingsMixin, InvenTreePlug BLOCKING_PRINT = True - SETTINGS = {} + SETTINGS = { + 'DEBUG': { + 'name': _('Debug mode'), + 'description': _('Enable debug mode - returns raw HTML instead of PDF'), + 'validator': bool, + 'default': False, + } + } PrintingOptionsSerializer = LabelPrintingOptionsSerializer + debug = None + + def in_debug_mode(self): + """Check if the plugin is printing in debug mode.""" + if self.debug is None: + self.debug = str2bool(self.get_setting('DEBUG')) def print_labels( self, label: LabelTemplate, output: LabelOutput, items: list, request, **kwargs @@ -135,11 +149,15 @@ class InvenTreeLabelSheetPlugin(LabelPrintingMixin, SettingsMixin, InvenTreePlug # Render to a single HTML document html_data = self.wrap_pages(pages, **document_data) - # Render HTML to PDF - html = weasyprint.HTML(string=html_data) - document = html.render().write_pdf() + if self.in_debug_mode(): + # Render HTML to PDF + html = weasyprint.HTML(string=html_data) + document = html.render().write_pdf() + + output.output = ContentFile(document, 'labels.pdf') + else: + output.output = ContentFile(html_data, 'labels.html') - output.output = ContentFile(document, 'labels.pdf') output.progress = 100 output.complete = True output.save()