Coverage for src/backend/InvenTree/plugin/samples/integration/label_sample.py: 0%
21 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 17:47 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 17:47 +0000
1"""Simple sample for a plugin with the LabelPrintingMixin.
3This does not function in real usage and is more to show the required components and for unit tests.
4"""
6from rest_framework import serializers
8from InvenTree.config import get_testfolder_dir
9from plugin import InvenTreePlugin
10from plugin.mixins import LabelPrintingMixin
13class SampleLabelPrinter(LabelPrintingMixin, InvenTreePlugin):
14 """Sample plugin which provides a 'fake' label printer endpoint."""
16 NAME = 'Sample Label Printer'
17 SLUG = 'samplelabelprinter'
18 TITLE = 'Sample Label Printer'
19 DESCRIPTION = 'A sample plugin which provides a (fake) label printer interface'
20 AUTHOR = 'InvenTree contributors'
21 VERSION = '0.3.0'
23 class PrintingOptionsSerializer(serializers.Serializer):
24 """Serializer to return printing options."""
26 amount = serializers.IntegerField(required=False, default=1)
28 def print_label(self, **kwargs):
29 """Sample printing step.
31 Normally here the connection to the printer and transfer of the label would take place.
32 """
33 # Test that the expected kwargs are present
34 print(f'Printing Label: {kwargs["filename"]} (User: {kwargs["user"]})')
36 pdf_data = kwargs['pdf_data']
37 png_file = self.render_to_png(
38 kwargs['label_instance'], kwargs['item_instance'], **kwargs
39 )
41 filename = str(get_testfolder_dir() / 'label.pdf')
43 # Dump the PDF to a local file
44 with open(filename, 'wb') as pdf_out:
45 pdf_out.write(pdf_data)
47 # Save the PNG to disk
48 png_file.save(filename.replace('.pdf', '.png'))