print_label.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/env python3
  2. """print_label.py - print a label with the name, the company and SEQ_HEX (in a barcode) of an attendee
  3. Copyright 2015-2013 Emiliano Mattioli <oloturia AT gmail.com>
  4. Davide Alberani <da@erlug.linux.it>
  5. RaspiBO <info@raspibo.org>
  6. Licensed under the Apache License 2.0
  7. """
  8. import os
  9. import sys
  10. import cups
  11. import tempfile
  12. from PIL import Image, ImageFont, ImageDraw
  13. KEEP_IMG = True
  14. LABEL_WIDTH = 13488
  15. LABEL_HEIGHT = 3744
  16. FONT_TEXT = 'Ubuntu-C.ttf'
  17. FONT_TEXT = 'CONCIBB_.TTF'
  18. FONT_TEXT_ENCODING = 'latin-1'
  19. FONT_BARCODE = 'free3of9.ttf'
  20. PRINTER_NAME = None
  21. #PRINTER_NAME = 'DYMO_LabelWriter_450'
  22. def _get_resource(filename):
  23. return os.path.join(os.path.dirname(sys.argv[0]), filename)
  24. def build_label(w, h, barcode_text, line1, line2, font_text=FONT_TEXT, font_barcode=FONT_BARCODE):
  25. barcode_text = "*" + barcode_text + "*"
  26. line1 = str(line1, 'utf-8').encode(FONT_TEXT_ENCODING, 'ignore')
  27. line2 = str(line2, 'utf-8').encode(FONT_TEXT_ENCODING, 'ignore')
  28. fontbar = ImageFont.truetype(_get_resource(font_barcode), 2000)
  29. fontname = ImageFont.truetype(_get_resource(font_text), 1100)
  30. fontjob = ImageFont.truetype(_get_resource(font_text), 780)
  31. image = Image.new('RGB', (w, h), (255, 255, 255))
  32. draw = ImageDraw.Draw(image)
  33. wbar, hbar = draw.textsize(barcode_text, font=fontbar)
  34. wnorm1, hnorm1 = draw.textsize(line1, font=fontname)
  35. wnorm2, hnorm2 = draw.textsize(line2, font=fontjob)
  36. draw.text(((w-wnorm1)/2, -1300+(h-hnorm1)/2), line1, (0, 0, 0), font=fontname)
  37. draw.text(((w-wnorm2)/2, -480+(h-hnorm2)/2), line2, (0, 0, 0), font=fontjob)
  38. draw.text(((w-wbar)/2, 850+(h-hbar)/2), barcode_text, (0, 0, 0), font=fontbar)
  39. if not KEEP_IMG:
  40. tmpfile = tempfile.NamedTemporaryFile(prefix='eventman_print_label_', suffix='.png')
  41. else:
  42. tmpfile = tempfile.mktemp(prefix='eventman_print_label_', suffix='.png')
  43. tmpfile = open(tmpfile, 'wb')
  44. image.save(tmpfile, dpi=[600, 300], format='png')
  45. return tmpfile
  46. def print_label(label_file, name):
  47. conn = cups.Connection()
  48. printer = PRINTER_NAME or conn.getDefault()
  49. conn.printFile(printer, label_file.name, name, {})
  50. def run():
  51. # Always consume stdin.
  52. sys.stdin.read()
  53. name = ' '.join([os.environ.get('NAME') or '', os.environ.get('SURNAME') or ''])
  54. company = os.environ.get('COMPANY') or ''
  55. # Print the decimal value SEQ as an hex of at least 6 digits.
  56. seq = os.environ.get('SEQ_HEX', '0')
  57. label_file = build_label(LABEL_WIDTH, LABEL_HEIGHT, seq, name, company)
  58. print_label(label_file, name)
  59. if __name__ == '__main__':
  60. try:
  61. run()
  62. except Exception as e:
  63. sys.stderr.write('print_label. Exception raised: %s\n' % e)