mise en gabarit de png / jpeg

This commit is contained in:
2024-06-16 18:17:57 +02:00
parent f2062441d3
commit 7bec4a86f0
3 changed files with 99 additions and 34 deletions

View File

@@ -3,11 +3,14 @@ from flask import Flask, render_template, request
import flask
import io
import zipfile
from werkzeug.datastructures import FileStorage
from PIL import Image
VALID_KIND = ["all", "tc", "zc", "zip"]
ALLOWED_EXTENSIONS = ["png", "jpeg", "jpg"]
def generateTemplate(width: int, height: int, fondPerdu: int, zoneTranquille: int, marge: int, kind: str = "all") -> fpdf.FPDF:
def generateTemplate(width: int, height: int, fondPerdu: int, zoneTranquille: int, marge: int, kind: str = "all", image: FileStorage = None, aFondPerdu: bool = False) -> fpdf.FPDF:
margeDuContenue = marge + fondPerdu
pdf = fpdf.FPDF("P", "mm", (width + margeDuContenue *
@@ -17,6 +20,17 @@ def generateTemplate(width: int, height: int, fondPerdu: int, zoneTranquille: in
pdf.set_auto_page_break(False)
pdf.add_page()
if (image):
if 'inMemoryImage' not in flask.g:
flask.g.inMemoryImage = io.BytesIO()
image.save(flask.g.inMemoryImage)
xy, w, h = margeDuContenue, width, height
if (aFondPerdu == True):
xy = xy - fondPerdu
w = w + fondPerdu*2
h = h + fondPerdu*2
pdf.image(flask.g.inMemoryImage, xy, xy, w, h, keep_aspect_ratio=True)
if (kind in ["all", "zc"]):
pdf.set_draw_color(255, 0, 0) # red
pdf.rect(margeDuContenue, margeDuContenue, width, height)
@@ -79,6 +93,11 @@ def generateTemplate(width: int, height: int, fondPerdu: int, zoneTranquille: in
return pdf
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
app = Flask(__name__)
@@ -87,24 +106,31 @@ def index():
return render_template("index.html")
@app.route("/generate")
@app.route("/generate", methods=['GET', 'POST'])
def generate():
try:
w = int(request.args.get("width", ""))
h = int(request.args.get("height", ""))
fp = int(request.args.get("fp", ""))
zt = int(request.args.get("zt", ""))
m = int(request.args.get("margin", ""))
k = request.args.get("kind", "all")
w = int(request.form.get("width", ""))
h = int(request.form.get("height", ""))
fp = int(request.form.get("fp", ""))
zt = int(request.form.get("zt", ""))
m = int(request.form.get("margin", ""))
k = request.form.get("kind", "all")
ffp = request.form.get("ffp", "off")
ffp = {"on": True, "off": False}[ffp]
if k not in VALID_KIND:
raise ValueError("kind is not valid")
image = None
if 'file' in request.files and allowed_file(request.files['file'].filename):
image = request.files['file']
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)
app.logger.debug(
"w %d ; h %d ; fp %d ; zt %d ; m %d ; ffp %s", w, h, fp, zt, m, ffp)
if k != "zip":
pdf = generateTemplate(w, h, fp, zt, m, k).output(dest="S")
pdf = generateTemplate(w, h, fp, zt, m, k, image,ffp).output(dest="S")
return flask.send_file(
io.BytesIO(pdf.encode("latin-1")),
io.BytesIO(pdf),
mimetype="application/pdf",
download_name=f'{w}x{h}_{k}.pdf',
)
@@ -113,8 +139,8 @@ def generate():
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'))
pdf = generateTemplate(w, h, fp, zt, m, kk, image,ffp).output(dest="S")
zipHandle.writestr(f'{w}x{h}_{kk}.pdf', pdf)
zipHandle.close()
return flask.send_file(
io.BytesIO(zipBuffer.getvalue()),

View File

@@ -1,2 +1,2 @@
flask
fpdf
fpdf2

View File

@@ -9,21 +9,25 @@
background-color: lightgray;
}
form {
font-size: larger;
display: grid;
width: 90%;
margin: 0;
position: absolute;
top: 50%;
left: 50%;
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
.two-col {
justify-content: center;
justify-items: left;
align-items: start;
align-content: center;
column-gap: 1ch;
display: grid;
grid-row: 1;
grid-column: auto;
}
form {
max-width: calc(100vw - 5ch);
width: fit-content;
font-size: larger;
padding: 1ch;
margin: auto;
border: solid 1px black;
overflow: auto;
}
label {
@@ -43,20 +47,55 @@
grid-column: 1 / span 2;
justify-self: normal;
}
input[type="file"] {
grid-column: 1 / span 2;
justify-self: normal;
}
.sectionTitle {
font-weight: bold;
margin-bottom: 1ch;
grid-column: 1 / span 2;
}
.pageTitle {
font-weight: bolder;
font-size: 5vw;
margin-top: 1ch;
margin-bottom: 1ch;
text-align: center;
}
label {
white-space: nowrap;
}
</style>
</head>
<body>
<form action="/generate" method="get">
<label for="width">Largeur (mm)</label><input required type="number" name="width" id="width" value="85">
<label for="height">Hauteur (mm)</label><input required type="number" name="height" id="height" value="54">
<label for="fp">Fond perdu (mm)</label><input required type="number" name="fp" id="fp" value="5">
<label for="zt">Zone tranquille (mm)</label><input required type="number" name="zt" id="zt" value="3">
<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">
<label for="rb_zip">ZIP</label><input required type="radio" value="zip" name="kind" id="rb_zip" checked>
<div class="pageTitle">Générateur de gabarit d'impression</div class="pageTitle">
<form action="/generate" method="post" enctype="multipart/form-data" class="two-col">
<div class="two-col">
<span class="sectionTitle">Configuration</span>
<label for="width">Largeur (mm)</label><input required type="number" name="width" id="width" value="85">
<label for="height">Hauteur (mm)</label><input required type="number" name="height" id="height" value="54">
<label for="fp">Fond perdu (mm)</label><input required type="number" name="fp" id="fp" value="5">
<label for="zt">Zone tranquille (mm)</label><input required type="number" name="zt" id="zt" value="3">
<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">
<label for="rb_zip">ZIP</label><input required type="radio" value="zip" name="kind" id="rb_zip" checked>
</div>
<div class="two-col">
<span>
<span class="sectionTitle">Mettre en gabarit</span>
<span><br />PNG ou JPEG uniquement</span></span>
<input type="file" name="file" accept="image/png, image/jpeg">
<label for="cb_fp">Fond perdu</label><input type="checkbox" name="ffp">
</div>
<input type="submit" value="Générer">
</form>
</body>