diff --git a/data/triggers-available/Ubuntu-C.ttf b/data/triggers-available/Ubuntu-C.ttf new file mode 100644 index 0000000..8d3e867 Binary files /dev/null and b/data/triggers-available/Ubuntu-C.ttf differ diff --git a/data/triggers-available/echo.py b/data/triggers-available/echo.py index 339e463..61dae42 100755 --- a/data/triggers-available/echo.py +++ b/data/triggers-available/echo.py @@ -6,9 +6,10 @@ import sys import json def main(): - print 'STDIN JSON:', json.loads(sys.stdin.read()) + # NOTE: even if not used, ALWAYS consume sys.stdin to close the pipe. + print 'STDIN JSON:', repr(json.loads(sys.stdin.read())) print '' - print 'ENV:', os.environ + print 'ENV:', repr(os.environ) if __name__ == '__main__': diff --git a/data/triggers-available/free3of9.ttf b/data/triggers-available/free3of9.ttf new file mode 100644 index 0000000..513afef Binary files /dev/null and b/data/triggers-available/free3of9.ttf differ diff --git a/data/triggers-available/print_label.py b/data/triggers-available/print_label.py new file mode 100755 index 0000000..d97c286 --- /dev/null +++ b/data/triggers-available/print_label.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python +"""print_label.py - print a label with the name, the company and the person_id (in a barcode) of an attendee + +Copyright 2015 Emiliano Mattioli + Davide Alberani + RaspiBO + +Licensed under the Apache License 2.0 +""" + +import os +import sys +import cups +import tempfile +from PIL import Image, ImageFont, ImageDraw + +LABEL_WIDTH = 13488 +LABEL_HEIGHT = 3744 + +FONT_TEXT = 'Ubuntu-C.ttf' +FONT_TEXT_ENCODING = 'latin-1' +FONT_BARCODE = 'free3of9.ttf' + +PRINTER_NAME = None + + + +def _get_resource(filename): + return os.path.join(os.path.dirname(sys.argv[0]), filename) + + +def build_label(w, h, barcode_text, line1, line2, font_text=FONT_TEXT, font_barcode=FONT_BARCODE): + 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) + fontnorm = ImageFont.truetype(_get_resource(font_text), 780) + image = Image.new('RGB', (w, h), (255, 255, 255)) + draw = ImageDraw.Draw(image) + wbar, hbar = draw.textsize(barcode_text, font=fontbar) + wnorm1, hnorm1 = draw.textsize(line1, font=fontnorm) + 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') + return tmpfile + + +def print_label(label_file, name): + conn = cups.Connection() + printer = PRINTER_NAME or conn.getDefault() + conn.printFile(printer, label_file.name, name, {}) + + +def run(): + # Always consume stdin. + 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_label(label_file, name) + + +if __name__ == '__main__': + try: + run() + except Exception, e: + sys.stderr.write('print_label. Exception raised: %s\n' % e) +