Label Printer
Label printer¶
Label printer machines can directly print labels for various items in InvenTree. They replace standard LabelPrintingMixin
plugins that are used to connect to physical printers. Using machines rather than a standard LabelPrintingMixin
plugin has the advantage that machines can be created multiple times using different settings but the same driver. That way multiple label printers of the same brand can be connected.
Writing your own printing driver¶
Take a look at the most basic required code for a driver in this example. Next either implement the print_label
or print_labels
function.
Label printer status¶
There are a couple of predefined status codes for label printers. By default the UNKNOWN
status code is set for each machine, but they can be changed at any time by the driver. For more info about status code see Machine status codes.
Attributes:
Name | Type | Description |
---|---|---|
CONNECTED |
The printer is connected and ready to print |
|
UNKNOWN |
The printer status is unknown (e.g. there is no active connection to the printer) |
|
PRINTING |
The printer is currently printing a label |
|
NO_MEDIA |
The printer is out of media (e.g. the label spool is empty) |
|
DISCONNECTED |
The driver cannot establish a connection to the printer |
Source code in src/backend/InvenTree/machine/machine_types/label_printer.py
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
|
LabelPrintingDriver API¶
Base driver for label printer machines.
Attributes:
Name | Type | Description |
---|---|---|
USE_BACKGROUND_WORKER |
bool
|
If True, the |
Source code in src/backend/InvenTree/machine/machine_types/label_printer.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
|
machine_plugin: LabelPrintingMixin
property
¶
Returns the builtin machine label printing plugin that manages printing through machines.
PrintingOptionsSerializer
¶
Printing options serializer that implements common options.
This can be overridden by the driver to implement custom options, but the driver should always extend this class.
Example
This example shows how to extend the default serializer and add a new option:
class MyDriver(LabelPrinterBaseDriver):
# ...
class PrintingOptionsSerializer(LabelPrinterBaseDriver.PrintingOptionsSerializer):
auto_cut = serializers.BooleanField(
default=True,
label=_('Auto cut'),
help_text=_('Automatically cut the label after printing'),
)
Source code in src/backend/InvenTree/machine/machine_types/label_printer.py
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
|
print_label(machine, label, item, request, **kwargs)
¶
Print a single label with the provided template and item.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
machine |
LabelPrinterMachine
|
The LabelPrintingMachine instance that should be used for printing |
required |
label |
LabelTemplate
|
The LabelTemplate object to use for printing |
required |
item |
LabelItemType
|
The database item to print (e.g. StockItem instance) |
required |
request |
Request
|
The HTTP request object which triggered this print job |
required |
Other Parameters:
Name | Type | Description |
---|---|---|
printing_options |
dict
|
The printing options set for this print job defined in the PrintingOptionsSerializer by default the following options are available: - copies: number of copies to print for the label |
Note that the supplied args/kwargs may be different if the driver overrides the print_labels() method.
Source code in src/backend/InvenTree/machine/machine_types/label_printer.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
|
print_labels(machine, label, items, request, **kwargs)
¶
Print one or more labels with the provided template and items.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
machine |
LabelPrinterMachine
|
The LabelPrintingMachine instance that should be used for printing |
required |
label |
LabelTemplate
|
The LabelTemplate object to use for printing |
required |
items |
QuerySet[LabelItemType]
|
The list of database items to print (e.g. StockItem instances) |
required |
request |
Request
|
The HTTP request object which triggered this print job |
required |
Other Parameters:
Name | Type | Description |
---|---|---|
printing_options |
dict
|
The printing options set for this print job defined in the PrintingOptionsSerializer by default the following options are available: - copies: number of copies to print for each label |
Returns:
Type | Description |
---|---|
Union[None, JsonResponse]
|
If |
The default implementation simply calls print_label() for each label, producing multiple single label output "jobs" but this can be overridden by the particular driver.
Source code in src/backend/InvenTree/machine/machine_types/label_printer.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
|
get_printers(label, items, **kwargs)
¶
Get all printers that would be available to print this job.
By default all printers that are initialized using this driver are returned.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
label |
LabelTemplate
|
The LabelTemplate object to use for printing |
required |
items |
QuerySet[LabelItemType]
|
The lost of database items to print (e.g. StockItem instances) |
required |
Other Parameters:
Name | Type | Description |
---|---|---|
request |
Request
|
The django request used to make the get printers request |
Source code in src/backend/InvenTree/machine/machine_types/label_printer.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
|
get_printing_options_serializer(request, *args, **kwargs)
¶
Return a serializer class instance with dynamic printing options.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request |
Request
|
The request made to print a label or interfering the available serializer fields via an OPTIONS request |
required |
Note
*args
, **kwargs
needs to be passed to the serializer instance
Returns:
Type | Description |
---|---|
PrintingOptionsSerializer
|
A class instance of a DRF serializer class, by default this an instance of self.PrintingOptionsSerializer using the args, *kwargs if existing for this driver |
Source code in src/backend/InvenTree/machine/machine_types/label_printer.py
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
|
render_to_pdf(label, item, request, **kwargs)
¶
Helper method to render a label to PDF format for a specific item.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
label |
LabelTemplate
|
The LabelTemplate object to render |
required |
item |
LabelItemType
|
The item to render the label with |
required |
request |
Request
|
The HTTP request object which triggered this print job |
required |
Source code in src/backend/InvenTree/machine/machine_types/label_printer.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
|
render_to_pdf_data(label, item, request, **kwargs)
¶
Helper method to render a label to PDF and return it as bytes for a specific item.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
label |
LabelTemplate
|
The LabelTemplate object to render |
required |
item |
LabelItemType
|
The item to render the label with |
required |
request |
Request
|
The HTTP request object which triggered this print job |
required |
Source code in src/backend/InvenTree/machine/machine_types/label_printer.py
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
|
render_to_html(label, item, request, **kwargs)
¶
Helper method to render a label to HTML format for a specific item.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
label |
LabelTemplate
|
The LabelTemplate object to render |
required |
item |
LabelItemType
|
The item to render the label with |
required |
request |
Request
|
The HTTP request object which triggered this print job |
required |
Source code in src/backend/InvenTree/machine/machine_types/label_printer.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
|
render_to_png(label, item, request, **kwargs)
¶
Helper method to render a label to PNG format for a specific item.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
label |
LabelTemplate
|
The LabelTemplate object to render |
required |
item |
LabelItemType
|
The item to render the label with |
required |
request |
Request
|
The HTTP request object which triggered this print job |
required |
Other Parameters:
Name | Type | Description |
---|---|---|
pdf_data |
bytes
|
The pdf document as bytes (optional) |
dpi |
int
|
The dpi used to render the image (optional) |
use_cairo |
bool
|
Whether to use the pdftocairo backend for rendering which provides better results in tests, see #6488 for details. If False, pdftoppm is used (default: True) |
pdf2image_kwargs |
dict
|
Additional keyword arguments to pass to the
|
Source code in src/backend/InvenTree/machine/machine_types/label_printer.py
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
|