Skip to content

Commit

Permalink
Added handling of ZPL templates
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeoLacruz committed Dec 11, 2024
1 parent a52be49 commit a7dd61c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 22 deletions.
2 changes: 1 addition & 1 deletion inventree_zebra/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ZEBRA_PLUGIN_VERSION = "0.7.0"
ZEBRA_PLUGIN_VERSION = "0.7.0.dev"
70 changes: 49 additions & 21 deletions inventree_zebra/zebra_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
Supports direct printing of labels on label printers
"""
import os

# translation
from django.utils.translation import gettext_lazy as _
from django.core.validators import MinValueValidator
from django.core.validators import MaxValueValidator
from django.conf import settings
from django.shortcuts import render

# InvenTree plugin libs
from plugin import InvenTreePlugin
Expand Down Expand Up @@ -44,6 +48,7 @@ class ZebraLabelPlugin(LabelPrintingMixin, SettingsMixin, InvenTreePlugin):
'PORT': {
'name': _('Port'),
'description': _('Network port in case of network printer'),
'validator': [int, MinValueValidator(0)],
'default': '9100',
},
'LOCAL_IF': {
Expand Down Expand Up @@ -85,7 +90,10 @@ def print_label(self, **kwargs):
threshold = self.get_setting('THRESHOLD')
dpmm = int(self.get_setting('DPMM'))
printer_init = self.get_setting('PRINTER_INIT')
label_image = kwargs['png_file']

# Extract width (x) and height (y) information.
width = kwargs['width']
height = kwargs['height']

# Select the right printer.
# This is a multi printer hack. In case the label has an IP address in the metadata
Expand All @@ -100,26 +108,46 @@ def print_label(self, **kwargs):
darkness = label.metadata['darkness']
except Exception:
darkness = self.get_setting('DARKNESS')

# Extract width (x) and height (y) information.
width = kwargs['width']
height = kwargs['height']

# Set the darkness
fn = lambda x: 255 if x > threshold else 0
label_image = label_image.convert('L').point(fn, mode='1')

# Uncomment this if you need the intermediate png file for debugging.
# label_image.save('/home/user/label.png')

# Convert image to Zebra zpl
li = zpl.Label(height, width, dpmm)
li.set_darkness(darkness)
li.labelhome(0, 0)
li.zpl_raw(printer_init)
li.origin(0, 0)
li.write_graphic(label_image, width)
li.endorigin()
try:
zpl_template = label.metadata['zpl_template']
except Exception:
zpl_template = False

# From here we need to distinguish between html templates and ZPL templates
if zpl_template:
# Find the template file and load its content
template_file = os.path.join(settings.MEDIA_ROOT, 'report/label')
template_file = os.path.join(template_file, str(kwargs['context']['template']))

# Render the template
fields = {'object': kwargs['item_instance']}
raw_zpl = str(render(None, template_file, fields).content)

# Create the zpl data
li = zpl.Label(height, width, dpmm)
li.set_darkness(darkness)
li.labelhome(0, 0)
li.zpl_raw(printer_init)
li.origin(0, 0)
li.zpl_raw(raw_zpl)
li.endorigin()
else:
# Set the threshold
label_image = kwargs['png_file']
fn = lambda x: 255 if x > threshold else 0
label_image = label_image.convert('L').point(fn, mode='1')

# Uncomment this if you need the intermediate png file for debugging.
# label_image.save('/home/user/label.png')

# Convert image to Zebra zpl
li = zpl.Label(height, width, dpmm)
li.set_darkness(darkness)
li.labelhome(0, 0)
li.zpl_raw(printer_init)
li.origin(0, 0)
li.write_graphic(label_image, width)
li.endorigin()

# Uncomment this if you need the intermediate zpl file for debugging.
# datafile=open('/home/user/label.txt','w')
Expand Down

0 comments on commit a7dd61c

Please sign in to comment.