Convert to PDF first
Getty Ritter
7 years ago
2 | 2 | |
3 | 3 | import base64 |
4 | 4 | import flask |
5 | import io | |
5 | 6 | import random |
6 | 7 | import openpyxl |
8 | import os | |
7 | 9 | import svgwrite |
10 | from svglib.svglib import svg2rlg | |
11 | from reportlab.graphics import renderPDF | |
8 | 12 | |
9 | 13 | app = flask.Flask(__name__) |
10 | 14 | |
28 | 32 | |
29 | 33 | |
30 | 34 | @app.route('/bingo.svg') |
35 | @app.route('/bingo.pdf') | |
31 | 36 | def generate(): |
32 | 37 | choice_str = base64.urlsafe_b64decode(flask.request.args.get('choices')) |
33 | 38 | choices = choice_str.decode('utf-8').split('\x1c') |
34 | 39 | print(choices) |
35 |
return mk_svg(choices) |
|
40 | return mk_svg(choices), 200, {'Content-Type': 'application/pdf'} | |
36 | 41 | |
37 | 42 | |
38 | 43 | def choices_from_excel(wb): |
48 | 53 | if len(choices) < 24: |
49 | 54 | raise Exception('Must have at least 24 possible choices!') |
50 | 55 | random.shuffle(choices) |
56 | ||
57 | # make the empty drawing | |
51 | 58 | d = svgwrite.Drawing(size=('8.5in', '11in')) |
52 | # t = svgwrite.mixins.Transform.translate('0.25in', '0.25in') | |
59 | ||
60 | # draw the lines | |
53 | 61 | for x in range(6): |
54 | 62 | d.add(d.line(('{0}in'.format(x*1.6), '0in'), |
55 | 63 | ('{0}in'.format(x*1.6), '8in'), |
56 |
stroke=svgwrite.rgb(0, 0, 0))).translate( |
|
64 | stroke=svgwrite.rgb(0, 0, 0))).translate(22.5, 22.5) | |
57 | 65 | for y in range(6): |
58 | 66 | d.add(d.line(('0in', '{0}in'.format(y*1.6)), |
59 | 67 | ('8in', '{0}in'.format(y*1.6)), |
60 |
stroke=svgwrite.rgb(0, 0, 0))).translate( |
|
68 | stroke=svgwrite.rgb(0, 0, 0))).translate(22.5, 22.5) | |
69 | ||
70 | # draw the text | |
61 | 71 | for x in range(5): |
62 | 72 | for y in range(5): |
63 | 73 | if x == 2 and y == 2: |
64 |
text = 'FREE |
|
74 | text = 'FREE SQUARE' | |
65 | 75 | color = '#b00' |
66 | 76 | else: |
67 | 77 | text = choices.pop() |
73 | 83 | text_anchor='middle', |
74 | 84 | alignment_baseline='central', |
75 | 85 | 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() | |
78 | 92 | |
79 | 93 | |
80 | 94 | 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() |