2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-05-02 13:28:49 +00:00

Add debug option to the sheet printer plugin (#7614)

This commit is contained in:
Miklós Márton 2024-07-11 05:28:29 +02:00 committed by GitHub
parent 5e040c4dc4
commit 4e6879407e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -12,6 +12,7 @@ import weasyprint
from rest_framework import serializers from rest_framework import serializers
import report.helpers import report.helpers
from InvenTree.helpers import str2bool
from plugin import InvenTreePlugin from plugin import InvenTreePlugin
from plugin.mixins import LabelPrintingMixin, SettingsMixin from plugin.mixins import LabelPrintingMixin, SettingsMixin
from report.models import LabelOutput, LabelTemplate from report.models import LabelOutput, LabelTemplate
@ -64,9 +65,22 @@ class InvenTreeLabelSheetPlugin(LabelPrintingMixin, SettingsMixin, InvenTreePlug
BLOCKING_PRINT = True 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 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( def print_labels(
self, label: LabelTemplate, output: LabelOutput, items: list, request, **kwargs self, label: LabelTemplate, output: LabelOutput, items: list, request, **kwargs
@ -135,11 +149,15 @@ class InvenTreeLabelSheetPlugin(LabelPrintingMixin, SettingsMixin, InvenTreePlug
# Render to a single HTML document # Render to a single HTML document
html_data = self.wrap_pages(pages, **document_data) html_data = self.wrap_pages(pages, **document_data)
# Render HTML to PDF if self.in_debug_mode():
html = weasyprint.HTML(string=html_data) # Render HTML to PDF
document = html.render().write_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.progress = 100
output.complete = True output.complete = True
output.save() output.save()