print_label.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/env python
  2. """print_label.py - print a label with the name, the company and the person_id (in a barcode) of an attendee
  3. Copyright 2015 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 = False
  14. LABEL_WIDTH = 13488
  15. LABEL_HEIGHT = 3744
  16. FONT_TEXT = 'Ubuntu-C.ttf'
  17. FONT_TEXT_ENCODING = 'latin-1'
  18. FONT_BARCODE = 'free3of9.ttf'
  19. PRINTER_NAME = None
  20. PRINTER_NAME = 'DYMO_LabelWriter_450'
  21. def _get_resource(filename):
  22. return os.path.join(os.path.dirname(sys.argv[0]), filename)
  23. def build_label(w, h, barcode_text, line1, line2, font_text=FONT_TEXT, font_barcode=FONT_BARCODE):
  24. barcode_text = "*" + barcode_text + "*"
  25. line1 = unicode(line1, 'utf-8').encode(FONT_TEXT_ENCODING, 'ignore')
  26. line2 = unicode(line2, 'utf-8').encode(FONT_TEXT_ENCODING, 'ignore')
  27. fontbar = ImageFont.truetype(_get_resource(font_barcode), 2200)
  28. fontnorm = ImageFont.truetype(_get_resource(font_text), 780)
  29. image = Image.new('RGB', (w, h), (255, 255, 255))
  30. draw = ImageDraw.Draw(image)
  31. wbar, hbar = draw.textsize(barcode_text, font=fontbar)
  32. wnorm1, hnorm1 = draw.textsize(line1, font=fontnorm)
  33. wnorm2, hnorm2 = draw.textsize(line2, font=fontnorm)
  34. draw.text(((w-wnorm1)/2, -1200+(h-hnorm1)/2), line1, (0, 0, 0), font=fontnorm)
  35. draw.text(((w-wnorm2)/2, -450+(h-hnorm2)/2), line2, (0, 0, 0), font=fontnorm)
  36. draw.text(((w-wbar)/2, 850+(h-hbar)/2), barcode_text, (0, 0, 0), font=fontbar)
  37. if not KEEP_IMG:
  38. tmpfile = tempfile.NamedTemporaryFile(prefix='eventman_print_label_', suffix='.png')
  39. else:
  40. tmpfile = tempfile.mktemp(prefix='eventman_print_label_', suffix='.png')
  41. tmpfile = open(tmpfile, 'wb')
  42. image.save(tmpfile, dpi=[600, 300], format='png')
  43. return tmpfile
  44. def print_label(label_file, name):
  45. conn = cups.Connection()
  46. printer = PRINTER_NAME or conn.getDefault()
  47. conn.printFile(printer, label_file.name, name, {})
  48. def run():
  49. # Always consume stdin.
  50. sys.stdin.read()
  51. name = ' '.join([os.environ.get('NAME') or '', os.environ.get('SURNAME') or ''])
  52. company = os.environ.get('COMPANY') or ''
  53. # Print the decimal value SEQ as an hex of at least 6 digits.
  54. seq = os.environ.get('SEQ_HEX', '0')
  55. label_file = build_label(LABEL_WIDTH, LABEL_HEIGHT, seq, name, company)
  56. print_label(label_file, name)
  57. if __name__ == '__main__':
  58. try:
  59. run()
  60. except Exception, e:
  61. sys.stderr.write('print_label. Exception raised: %s\n' % e)