From 777d66f6a876f8fe7de0a43ef20ad2df006d44db Mon Sep 17 00:00:00 2001 From: Mitesh Ashar Date: Sat, 27 Jul 2013 09:06:29 +0530 Subject: [PATCH] Styled up label printing. #42 --- peopleflow/helpers/printlabel.py | 33 +++++++++++++++++++------------- peopleflow/views/participant.py | 4 ++-- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/peopleflow/helpers/printlabel.py b/peopleflow/helpers/printlabel.py index 7e76d00..0c217bc 100644 --- a/peopleflow/helpers/printlabel.py +++ b/peopleflow/helpers/printlabel.py @@ -6,7 +6,9 @@ from reportlab.lib.units import mm from reportlab.lib.enums import TA_CENTER from reportlab.platypus import SimpleDocTemplate, Paragraph +from reportlab.platypus.flowables import HRFlowable from reportlab.lib.styles import ParagraphStyle +from reportlab.lib.colors import Color def printlabel(printer, lines): @@ -15,25 +17,26 @@ def printlabel(printer, lines): fname = f.name f.close() + heights = [18, 32, 38] + doc = SimpleDocTemplate(fname, - pagesize=(62 * mm, 40 * mm), + pagesize=(62 * mm, heights[len(lines) - 1] * mm), topMargin=0, leftMargin=0, rightMargin=0, bottomMargin=0) story = [] styles = [ - ParagraphStyle("s1", fontName="Helvetica", alignment=TA_CENTER, fontSize=18, leading=18), - ParagraphStyle("s2", fontName="Helvetica", alignment=TA_CENTER, fontSize=13, leading=13, spaceBefore=6), - ParagraphStyle("s3", fontName="Helvetica", alignment=TA_CENTER, fontSize=13, leading=13, spaceBefore=6), - ParagraphStyle("s4", fontName="Helvetica", alignment=TA_CENTER, fontSize=13, leading=13, spaceBefore=6) + ParagraphStyle("s1", fontName="Helvetica-Bold", alignment=TA_CENTER, fontSize=15, leading=15), + ParagraphStyle("s2", fontName="Helvetica", alignment=TA_CENTER, fontSize=15, leading=18, spaceBefore=4, textColor=u"#555555"), + ParagraphStyle("s3", fontName="Helvetica", alignment=TA_CENTER, fontSize=12, leading=13, spaceBefore=4, textColor=u"#777777") ] - for i, line in enumerate(lines): - story.append(Paragraph(line, styles[i])) - # story.append(Paragraph(line2, style2)) + if i < len(styles): + story.append(Paragraph(line, styles[i])) + if i == 0 and len(lines) > 1: + story.append(HRFlowable(width='95%', color=u"#333333", spaceBefore=5)) doc.build(story) - #os.system("gnome-open " + fname) os.system("lpr -P %s %s" % (printer, fname)) time.sleep(2) os.unlink(fname) @@ -41,16 +44,20 @@ def printlabel(printer, lines): def make_label_content(participant): data = [participant.name] if participant.company: - data.append(participant.company) + compline = participant.company if participant.job: - data.append(participant.job) + compline = u"%s, %s" % (participant.job, compline) + data.append(compline) if(participant.twitter): data.append('@' + participant.twitter) return data if __name__ == '__main__': - parser = argparse.ArgumentParser(description='Script to print labels') + parser = argparse.ArgumentParser(description='Script to test printing of labels. Tries to test through fairly large 2-liners for name and job+company.') parser.add_argument('printer', type=str, help='The name of the printer') + parser.add_argument('--lines', type=int, help='The number of lines to print', default=3) args = parser.parse_args() - printlabel(args.printer, ["Kiran Jonnalagadda isn't long enough", "@jackerhack"]) + data = ["Kiran Jonnalagadda isn't long enough", "CEO, HasGeek Media LLP.", "@jackerhack"] + data = data[:args.lines] + printlabel(args.printer, data) diff --git a/peopleflow/views/participant.py b/peopleflow/views/participant.py index 0da8c30..a296d1e 100644 --- a/peopleflow/views/participant.py +++ b/peopleflow/views/participant.py @@ -76,11 +76,11 @@ def print_card(participant): try: if 'PRINTER_NAME' in app.config: printlabel(app.config['PRINTER_NAME'], make_label_content(participant)) - return jsonify(status=True, msg=u"Card for %s queued for printing" % participant.name) + return jsonify(status=True, msg=u"Label for %s queued for printing" % participant.name) else: return jsonify(status=False, msg=u"Printer not configured") except: - return jsonify(status=False, msg=u"Card for %s queued for printing" % participant.name) + return jsonify(status=False, msg=u"There was an error in printing the label for %s" % participant.name) @app.route('//search', methods=['POST']) @load_model(Event,{'id':'eid'},'event')