From f280ec56afd892c0ef23a5048dc130649b9bd620 Mon Sep 17 00:00:00 2001 From: AR2000 Date: Sat, 19 Oct 2024 17:18:58 +0200 Subject: [PATCH] =?UTF-8?q?Supporte=20les=20PDF=20comme=20images=20=C3=A0?= =?UTF-8?q?=20mettre=20en=20gabarit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Dockerfile | 1 + app/app.py | 41 ++++++++++++++++++++++++++++++++-------- app/requirements.txt | 4 +++- app/templates/index.html | 10 ++++++---- docker-compose.yml | 4 +++- 5 files changed, 46 insertions(+), 14 deletions(-) diff --git a/app/Dockerfile b/app/Dockerfile index 10d88b0..2b29ec9 100644 --- a/app/Dockerfile +++ b/app/Dockerfile @@ -3,6 +3,7 @@ FROM python:3.10-alpine WORKDIR /app COPY requirements.txt /app +RUN apk add py3-pdf2image RUN --mount=type=cache,target=/root/.cache/pip \ pip3 install -r requirements.txt diff --git a/app/app.py b/app/app.py index 1988fd6..962cb1f 100644 --- a/app/app.py +++ b/app/app.py @@ -5,9 +5,23 @@ import io import zipfile from werkzeug.datastructures import FileStorage from PIL import Image +from pdf2image import convert_from_bytes +from PyPDF2 import PdfReader VALID_KIND = ["all", "tc", "zc", "zip"] -ALLOWED_EXTENSIONS = ["png", "jpeg", "jpg"] +ALLOWED_EXTENSIONS = ["png", "jpeg", "jpg", "pdf"] + +# Function to get the dimensions of the source PDF page + + +def get_page_dimensions(pdfData, page_number=0): + reader = PdfReader(pdfData) + page = reader.pages[page_number] + width = float(page.mediabox.width) + height = float(page.mediabox.height) + width = width * (page.user_unit / 72) * 25.4 + height = height * (page.user_unit / 72) * 25.4 + return width, height def generateTemplate(width: int, height: int, fondPerdu: int, zoneTranquille: int, marge: int, kind: str = "all", image: FileStorage = None, aFondPerdu: bool = False) -> fpdf.FPDF: @@ -20,15 +34,27 @@ def generateTemplate(width: int, height: int, fondPerdu: int, zoneTranquille: in pdf.set_auto_page_break(False) pdf.add_page() - if (image): + xy, w, h = margeDuContenue, width, height + if (aFondPerdu == True): + xy = xy - fondPerdu + w = w + fondPerdu*2 + h = h + fondPerdu*2 + + if (image and image.mimetype in ["image/jpeg", "image/png"]): 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 + + if (image and image.mimetype in ["application/pdf", "application/x-pdf"]): + if 'inMemoryImage' not in flask.g: + pdfData = image.stream.read() + pdfW, pdfH = get_page_dimensions(io.BytesIO(pdfData)) + scale = min(pdfW/w, pdfH/h) + imageFromPDF = convert_from_bytes( + pdfData, dpi=600, size=(w*scale, h*scale), single_file=True) + flask.g.inMemoryImage = imageFromPDF[0] + + if ('inMemoryImage' in flask.g): pdf.image(flask.g.inMemoryImage, xy, xy, w, h, keep_aspect_ratio=True) if (kind in ["all", "zc"]): @@ -151,4 +177,3 @@ def generate(): if __name__ == '__main__': app.run(host='0.0.0.0', port=8000) - diff --git a/app/requirements.txt b/app/requirements.txt index 4112cf9..d392ccc 100644 --- a/app/requirements.txt +++ b/app/requirements.txt @@ -1,2 +1,4 @@ flask -fpdf2 \ No newline at end of file +fpdf2 +PyPDF2 +pdf2image \ No newline at end of file diff --git a/app/templates/index.html b/app/templates/index.html index 769052a..5532365 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -86,14 +86,16 @@ - - + +
Mettre en gabarit -
PNG ou JPEG uniquement
- +
PNG, JPEG ou PDF uniquement
+
diff --git a/docker-compose.yml b/docker-compose.yml index f1ae2f6..41845ca 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,6 +7,8 @@ services: # flask requires SIGINT to stop gracefully # (default stop signal from Compose is SIGTERM) stop_signal: SIGINT + ports: + - 5000:8000 labels: traefik.enable: true - traefik.http.routers.templatepdf.rule: Host(`template.ar2000.me`) \ No newline at end of file + traefik.http.routers.templatepdf.rule: Host(`template.ar2000.me`)> \ No newline at end of file