Enhance PDF generation to include multiple paragraphs before and after the image
This commit is contained in:
@@ -24,11 +24,13 @@ def generate_pdf():
|
|||||||
# Decide which language to use based on the query parameter; default to English
|
# Decide which language to use based on the query parameter; default to English
|
||||||
lang = request.args.get('lang', 'en').lower()
|
lang = request.args.get('lang', 'en').lower()
|
||||||
if lang == 'de':
|
if lang == 'de':
|
||||||
text = fake_de.text()
|
text = "\n\n".join([fake_de.text() for _ in range(4)]) # Generate 4 paragraphs
|
||||||
|
text_after_image = "\n\n".join([fake_de.text() for _ in range(2)]) # Generate 2 paragraphs
|
||||||
else:
|
else:
|
||||||
text = fake_en.text()
|
text = "\n\n".join([fake_en.text() for _ in range(4)]) # Generate 4 paragraphs
|
||||||
|
text_after_image = "\n\n".join([fake_en.text() for _ in range(2)]) # Generate 2 paragraphs
|
||||||
|
|
||||||
# Fetch a random image from picsum (you could also use Unsplash or other services)
|
# Fetch a random image from picsum
|
||||||
image_url = "https://picsum.photos/600/400"
|
image_url = "https://picsum.photos/600/400"
|
||||||
response = requests.get(image_url)
|
response = requests.get(image_url)
|
||||||
image_data = response.content # raw bytes
|
image_data = response.content # raw bytes
|
||||||
@@ -37,25 +39,37 @@ def generate_pdf():
|
|||||||
pdf_buffer = io.BytesIO()
|
pdf_buffer = io.BytesIO()
|
||||||
pdf_canvas = canvas.Canvas(pdf_buffer, pagesize=A4)
|
pdf_canvas = canvas.Canvas(pdf_buffer, pagesize=A4)
|
||||||
|
|
||||||
# Add text
|
# Add text before the image
|
||||||
pdf_canvas.setFont("Helvetica", 12)
|
pdf_canvas.setFont("Helvetica", 12)
|
||||||
pdf_canvas.drawString(50, 780, "Random PDF Generator")
|
text_object = pdf_canvas.beginText(50, 780) # Start at the top-left corner
|
||||||
pdf_canvas.drawString(50, 760, f"Language: {lang}")
|
text_object.setTextOrigin(50, 780)
|
||||||
text_lines = text.split('\n')
|
text_object.setFont("Helvetica", 12)
|
||||||
y_position = 740
|
|
||||||
for line in text_lines:
|
# Wrap and add the paragraphs
|
||||||
pdf_canvas.drawString(50, y_position, line)
|
for paragraph in text.split("\n\n"):
|
||||||
y_position -= 15
|
for line in paragraph.splitlines():
|
||||||
|
text_object.textLine(line)
|
||||||
|
text_object.textLine("") # Add a blank line between paragraphs
|
||||||
|
|
||||||
|
pdf_canvas.drawText(text_object)
|
||||||
|
|
||||||
# Embed the image
|
# Embed the image
|
||||||
# Use ImageReader to process the BytesIO object
|
|
||||||
img_buffer = io.BytesIO(image_data)
|
img_buffer = io.BytesIO(image_data)
|
||||||
img_reader = ImageReader(img_buffer) # Process the image data
|
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)
|
pdf_canvas.drawImage(img_reader, 50, 400, width=4 * inch, height=3 * inch)
|
||||||
|
|
||||||
# finalize
|
# Add text after the image
|
||||||
|
text_object = pdf_canvas.beginText(50, 380) # Start below the image
|
||||||
|
text_object.setFont("Helvetica", 12)
|
||||||
|
|
||||||
|
for paragraph in text_after_image.split("\n\n"):
|
||||||
|
for line in paragraph.splitlines():
|
||||||
|
text_object.textLine(line)
|
||||||
|
text_object.textLine("") # Add a blank line between paragraphs
|
||||||
|
|
||||||
|
pdf_canvas.drawText(text_object)
|
||||||
|
|
||||||
|
# Finalize the PDF
|
||||||
pdf_canvas.showPage()
|
pdf_canvas.showPage()
|
||||||
pdf_canvas.save()
|
pdf_canvas.save()
|
||||||
pdf_buffer.seek(0)
|
pdf_buffer.seek(0)
|
||||||
|
|||||||
Reference in New Issue
Block a user