print incremental sequence in labels

This commit is contained in:
Davide Alberani 2015-05-01 16:30:26 +02:00
parent d3ca5f342e
commit eb7b36f54e

View file

@ -14,6 +14,7 @@ import cups
import tempfile
from PIL import Image, ImageFont, ImageDraw
KEEP_IMG = False
LABEL_WIDTH = 13488
LABEL_HEIGHT = 3744
@ -22,7 +23,7 @@ FONT_TEXT_ENCODING = 'latin-1'
FONT_BARCODE = 'free3of9.ttf'
PRINTER_NAME = None
#PRINTER_NAME = 'DYMO_LabelWriter_450'
def _get_resource(filename):
@ -33,7 +34,7 @@ def build_label(w, h, barcode_text, line1, line2, font_text=FONT_TEXT, font_barc
barcode_text = "*" + barcode_text + "*"
line1 = unicode(line1, 'utf-8').encode(FONT_TEXT_ENCODING, 'ignore')
line2 = unicode(line2, 'utf-8').encode(FONT_TEXT_ENCODING, 'ignore')
fontbar = ImageFont.truetype(_get_resource(font_barcode), 1200)
fontbar = ImageFont.truetype(_get_resource(font_barcode), 2200)
fontnorm = ImageFont.truetype(_get_resource(font_text), 780)
image = Image.new('RGB', (w, h), (255, 255, 255))
draw = ImageDraw.Draw(image)
@ -42,9 +43,13 @@ def build_label(w, h, barcode_text, line1, line2, font_text=FONT_TEXT, font_barc
wnorm2, hnorm2 = draw.textsize(line2, font=fontnorm)
draw.text(((w-wnorm1)/2, -1200+(h-hnorm1)/2), line1, (0, 0, 0), font=fontnorm)
draw.text(((w-wnorm2)/2, -450+(h-hnorm2)/2), line2, (0, 0, 0), font=fontnorm)
draw.text(((w-wbar)/2, 750+(h-hbar)/2), barcode_text, (0, 0, 0), font=fontbar)
tmpfile = tempfile.NamedTemporaryFile(prefix='eventman_print_label_', suffix='.png')
image.save(tmpfile, dpi=[600,300], format='png')
draw.text(((w-wbar)/2, 850+(h-hbar)/2), barcode_text, (0, 0, 0), font=fontbar)
if not KEEP_IMG:
tmpfile = tempfile.NamedTemporaryFile(prefix='eventman_print_label_', suffix='.png')
else:
tmpfile = tempfile.mktemp(prefix='eventman_print_label_', suffix='.png')
tmpfile = open(tmpfile, 'wb')
image.save(tmpfile, dpi=[600, 300], format='png')
return tmpfile
@ -59,8 +64,9 @@ def run():
sys.stdin.read()
name = ' '.join([os.environ.get('NAME') or '', os.environ.get('SURNAME') or ''])
company = os.environ.get('COMPANY') or ''
person_id = os.environ.get('PERSON_ID', '').upper()
label_file = build_label(LABEL_WIDTH, LABEL_HEIGHT, person_id, name, company)
# Print the decimal value SEQ as an hex of at least 6 digits.
seq = os.environ.get('SEQ_HEX', '0')
label_file = build_label(LABEL_WIDTH, LABEL_HEIGHT, seq, name, company)
print_label(label_file, name)