From 66935e66577bccdefed0000cc1e7d023f0b05a37 Mon Sep 17 00:00:00 2001 From: Christian Krakau-Louis Date: Thu, 27 Mar 2025 16:25:06 +0100 Subject: [PATCH] Refactor PDF generation to use ImageReader for image processing --- app.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/app.py b/app.py index d95dbca..152c30a 100644 --- a/app.py +++ b/app.py @@ -5,6 +5,7 @@ from reportlab.pdfgen import canvas from reportlab.lib.pagesizes import A4 from reportlab.lib.units import inch from faker import Faker +from reportlab.lib.utils import ImageReader # Import ImageReader app = Flask(__name__) @@ -47,12 +48,12 @@ def generate_pdf(): y_position -= 15 # Embed the image - # First, write the downloaded image to an in-memory buffer - # Note: For certain image formats, we might need to use PIL to decode properly, - # but if the response is a jpeg from picsum, this simple approach works. + # Use ImageReader to process the BytesIO object img_buffer = io.BytesIO(image_data) - # let's place the image below the text - pdf_canvas.drawImage(img_buffer, 50, 400, width=4*inch, height=3*inch) + img_reader = ImageReader(img_buffer) # Process the image data + + # Place the image below the text + pdf_canvas.drawImage(img_reader, 50, 400, width=4*inch, height=3*inch) # finalize pdf_canvas.showPage()