gdritter repos bingo / d21a865
Convert to PDF first Getty Ritter 6 years ago
1 changed file(s) with 26 addition(s) and 9 deletion(s). Collapse all Expand all
22
33 import base64
44 import flask
5 import io
56 import random
67 import openpyxl
8 import os
79 import svgwrite
10 from svglib.svglib import svg2rlg
11 from reportlab.graphics import renderPDF
812
913 app = flask.Flask(__name__)
1014
2832
2933
3034 @app.route('/bingo.svg')
35 @app.route('/bingo.pdf')
3136 def generate():
3237 choice_str = base64.urlsafe_b64decode(flask.request.args.get('choices'))
3338 choices = choice_str.decode('utf-8').split('\x1c')
3439 print(choices)
35 return mk_svg(choices)
40 return mk_svg(choices), 200, {'Content-Type': 'application/pdf'}
3641
3742
3843 def choices_from_excel(wb):
4853 if len(choices) < 24:
4954 raise Exception('Must have at least 24 possible choices!')
5055 random.shuffle(choices)
56
57 # make the empty drawing
5158 d = svgwrite.Drawing(size=('8.5in', '11in'))
52 # t = svgwrite.mixins.Transform.translate('0.25in', '0.25in')
59
60 # draw the lines
5361 for x in range(6):
5462 d.add(d.line(('{0}in'.format(x*1.6), '0in'),
5563 ('{0}in'.format(x*1.6), '8in'),
56 stroke=svgwrite.rgb(0, 0, 0))).translate(45, 45)
64 stroke=svgwrite.rgb(0, 0, 0))).translate(22.5, 22.5)
5765 for y in range(6):
5866 d.add(d.line(('0in', '{0}in'.format(y*1.6)),
5967 ('8in', '{0}in'.format(y*1.6)),
60 stroke=svgwrite.rgb(0, 0, 0))).translate(45, 45)
68 stroke=svgwrite.rgb(0, 0, 0))).translate(22.5, 22.5)
69
70 # draw the text
6171 for x in range(5):
6272 for y in range(5):
6373 if x == 2 and y == 2:
64 text = 'FREE\nSQUARE'
74 text = 'FREE SQUARE'
6575 color = '#b00'
6676 else:
6777 text = choices.pop()
7383 text_anchor='middle',
7484 alignment_baseline='central',
7585 style=TEXT_STYLE,
76 fill=color)).translate(45, 45)
77 return d.tostring()
86 fill=color)).translate(22.5, 22.5)
87
88 buf = io.StringIO(d.tostring())
89 r = svg2rlg(buf)
90 return renderPDF.drawToString(r)
91 # return d.tostring()
7892
7993
8094 if __name__ == '__main__':
81 from flup.server.fcgi import WSGIServer
82 WSGIServer(app, bindAddress='/var/run/www/bingo.sock').run()
95 if os.getenv('DEBUG'):
96 app.run()
97 else:
98 from flup.server.fcgi import WSGIServer
99 WSGIServer(app, bindAddress='/var/run/www/bingo.sock').run()