Add zip download
This commit is contained in:
41
app/app.py
41
app/app.py
@@ -2,13 +2,9 @@ import fpdf
|
||||
from flask import Flask, render_template, request
|
||||
import flask
|
||||
import io
|
||||
import zipfile
|
||||
|
||||
WIDTH = 84
|
||||
HEIGHT = 54
|
||||
|
||||
FOND_PERDU = 5
|
||||
ZONE_TRANQUILLE = 3
|
||||
MARGE = 10
|
||||
VALID_KIND = ["all", "tc", "zc", "zip"]
|
||||
|
||||
|
||||
def generateTemplate(width: int, height: int, fondPerdu: int, zoneTranquille: int, marge: int, kind: str = "all") -> fpdf.FPDF:
|
||||
@@ -83,9 +79,6 @@ def generateTemplate(width: int, height: int, fondPerdu: int, zoneTranquille: in
|
||||
return pdf
|
||||
|
||||
|
||||
generateTemplate(WIDTH, HEIGHT, FOND_PERDU,
|
||||
ZONE_TRANQUILLE, MARGE).output("test.pdf")
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
|
||||
@@ -103,19 +96,31 @@ def generate():
|
||||
zt = int(request.args.get("zt", ""))
|
||||
m = int(request.args.get("margin", ""))
|
||||
k = request.args.get("kind", "all")
|
||||
if k not in ["all", "tc", "zc"]:
|
||||
if k not in VALID_KIND:
|
||||
raise ValueError("kind is not valid")
|
||||
except (KeyError, ValueError) as e:
|
||||
flask.abort(400)
|
||||
app.logger.debug("w %d ; h %d ; fp %d ; zt %d ; m %d", w, h, fp, zt, m)
|
||||
pdf = generateTemplate(w, h, fp, zt, m, k).output(dest="S")
|
||||
# generateTemplate(w, h, fp, zt, m).output(name="test.pdf")
|
||||
pdf = pdf.encode("latin-1")
|
||||
return flask.send_file(
|
||||
io.BytesIO(pdf),
|
||||
mimetype="application/pdf",
|
||||
download_name=f'{w}x{h}_{k}.pdf',
|
||||
)
|
||||
if k != "zip":
|
||||
pdf = generateTemplate(w, h, fp, zt, m, k).output(dest="S")
|
||||
return flask.send_file(
|
||||
io.BytesIO(pdf.encode("latin-1")),
|
||||
mimetype="application/pdf",
|
||||
download_name=f'{w}x{h}_{k}.pdf',
|
||||
)
|
||||
else:
|
||||
zipBuffer = io.BytesIO()
|
||||
zipHandle = zipfile.ZipFile(
|
||||
zipBuffer, "a", zipfile.ZIP_DEFLATED, False, 9)
|
||||
for kk in [x for x in VALID_KIND if x != "zip"]:
|
||||
pdf = generateTemplate(w, h, fp, zt, m, kk).output(dest="S")
|
||||
zipHandle.writestr(f'{w}x{h}_{kk}.pdf', pdf.encode('latin-1'))
|
||||
zipHandle.close()
|
||||
return flask.send_file(
|
||||
io.BytesIO(zipBuffer.getvalue()),
|
||||
mimetype="application/zip",
|
||||
download_name=f'{w}x{h}.zip',
|
||||
)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
@@ -51,7 +51,8 @@
|
||||
<label for="margin">Marge (mm)</label><input required type="number" name="margin" id="margin" value="10">
|
||||
<label for="rb_tc">Traits de coupe</label><input required type="radio" value="tc" name="kind" id="rb_tc">
|
||||
<label for="rb_zc">Zone de construction</label><input required type="radio" value="zc" name="kind" id="rb_zc">
|
||||
<label for="rb_all">Tout</label><input required type="radio" value="all" name="kind" id="rb_all" checked>
|
||||
<label for="rb_all">Tout</label><input required type="radio" value="all" name="kind" id="rb_all">
|
||||
<label for="rb_zip">ZIP</label><input required type="radio" value="zip" name="kind" id="rb_zip" checked>
|
||||
<input type="submit" value="Générer">
|
||||
</form>
|
||||
</body>
|
Reference in New Issue
Block a user