Improve PDF generation by adding text wrapping for paragraphs before and after images

This commit is contained in:
Christian Krakau-Louis
2025-03-27 16:40:21 +01:00
parent 7da2a63b25
commit a6071f1ab9
+11 -4
View File
@@ -6,6 +6,10 @@ from reportlab.lib.pagesizes import A4
from reportlab.lib.units import inch
from faker import Faker
from reportlab.lib.utils import ImageReader # Import ImageReader
import textwrap
# Set maximum line width in points (A4 page width is 595 pt, margins ~50 pt)
max_width = 480 # You can tweak this as needed
app = Flask(__name__)
@@ -45,12 +49,13 @@ def generate_pdf():
text_object.setTextOrigin(50, 780)
text_object.setFont("Helvetica", 12)
# Wrap and add the paragraphs
# Wrap and add the paragraphs
for paragraph in text.split("\n\n"):
for line in paragraph.splitlines():
wrapped_lines = textwrap.wrap(paragraph, width=90) # Adjust width to taste
for line in wrapped_lines:
text_object.textLine(line)
text_object.textLine("") # Add a blank line between paragraphs
pdf_canvas.drawText(text_object)
# Embed the image
@@ -61,12 +66,14 @@ def generate_pdf():
# Add text after the image
text_object = pdf_canvas.beginText(50, 380) # Start below the image
text_object.setFont("Helvetica", 12)
# Wrap and add the paragraphs
for paragraph in text_after_image.split("\n\n"):
for line in paragraph.splitlines():
wrapped_lines = textwrap.wrap(paragraph, width=90) # Adjust width to taste
for line in wrapped_lines:
text_object.textLine(line)
text_object.textLine("") # Add a blank line between paragraphs
# Draw the text object on the canvas
pdf_canvas.drawText(text_object)
# Finalize the PDF