Refactor PDF generation to use ImageReader for image processing
This commit is contained in:
@@ -5,6 +5,7 @@ from reportlab.pdfgen import canvas
|
|||||||
from reportlab.lib.pagesizes import A4
|
from reportlab.lib.pagesizes import A4
|
||||||
from reportlab.lib.units import inch
|
from reportlab.lib.units import inch
|
||||||
from faker import Faker
|
from faker import Faker
|
||||||
|
from reportlab.lib.utils import ImageReader # Import ImageReader
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
@@ -47,12 +48,12 @@ def generate_pdf():
|
|||||||
y_position -= 15
|
y_position -= 15
|
||||||
|
|
||||||
# Embed the image
|
# Embed the image
|
||||||
# First, write the downloaded image to an in-memory buffer
|
# Use ImageReader to process the BytesIO object
|
||||||
# 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.
|
|
||||||
img_buffer = io.BytesIO(image_data)
|
img_buffer = io.BytesIO(image_data)
|
||||||
# let's place the image below the text
|
img_reader = ImageReader(img_buffer) # Process the image data
|
||||||
pdf_canvas.drawImage(img_buffer, 50, 400, width=4*inch, height=3*inch)
|
|
||||||
|
# Place the image below the text
|
||||||
|
pdf_canvas.drawImage(img_reader, 50, 400, width=4*inch, height=3*inch)
|
||||||
|
|
||||||
# finalize
|
# finalize
|
||||||
pdf_canvas.showPage()
|
pdf_canvas.showPage()
|
||||||
|
|||||||
Reference in New Issue
Block a user