Hey,
Great question! I’ve had some success with manually explaining what I want via font type, spacing and such. I’ve found, in python at least, if you define the margin and width in the code that it helps. Additionally, if you add the draw_wrapped_text function that it also makes sure that your text wraps around, but that is the furthest I’ve gotten as of right now.
From the code below you get this:
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
# Create a PDF file
file_name = "example_thesis_reportlab.pdf"
c = canvas.Canvas(file_name, pagesize=letter)
# Define margin and width
margin_left = 50
margin_top = 750
max_width = 4 * inch
line_height = 20
# Function to wrap text
def wrap_text(text, max_width, font_name, font_size):
words = text.split()
lines = []
current_line = []
for word in words:
test_line = ' '.join(current_line + [word])
width = pdfmetrics.stringWidth(test_line, font_name, font_size)
if width <= max_width:
current_line.append(word)
else:
lines.append(' '.join(current_line))
current_line = [word]
if current_line:
lines.append(' '.join(current_line))
return lines
# Function to draw wrapped text
def draw_wrapped_text(c, text, x, y, max_width, font_name="Helvetica", font_size=12):
c.setFont(font_name, font_size)
lines = wrap_text(text, max_width, font_name, font_size)
for line in lines:
c.drawString(x, y, line)
y -= line_height
return y # Return the new y position
# Title Page
c.setFont("Helvetica-Bold", 16)
c.drawString(margin_left, margin_top, "Title: The Impact of Climate Change on Global Agriculture")
c.setFont("Helvetica", 12)
c.drawString(margin_left, margin_top - line_height, "Author: John Doe")
c.drawString(margin_left, margin_top - 2 * line_height, "Date: September 9, 2024")
c.showPage()
# Abstract
c.setFont("Helvetica-Bold", 14)
c.drawString(margin_left, margin_top, "Abstract")
abstract_text = "This thesis explores the various impacts of climate change on global agriculture, including changes in crop yields, water availability, and pest dynamics. The research synthesizes data from multiple sources to provide a comprehensive analysis of the challenges faced by the agricultural sector in the context of a changing climate. The findings suggest significant regional variations in the effects of climate change on agriculture, with some areas experiencing increased productivity while others face severe challenges. This study aims to contribute to the development of adaptive strategies for sustainable agriculture in the face of climate change."
draw_wrapped_text(c, abstract_text, margin_left, margin_top - line_height, max_width)
c.showPage()
# Table of Contents
c.setFont("Helvetica-Bold", 14)
c.drawString(margin_left, margin_top, "Table of Contents")
toc_items = [
"1. Introduction",
"2. Literature Review",
"3. Methodology",
"4. Results",
"5. Discussion",
"6. Conclusion",
"7. References"
]
for i, item in enumerate(toc_items):
c.setFont("Helvetica", 12)
c.drawString(margin_left, margin_top - (i + 1) * line_height, item)
c.showPage()
# Introduction
c.setFont("Helvetica-Bold", 14)
c.drawString(margin_left, margin_top, "1. Introduction")
intro_text = "Climate change has emerged as one of the most pressing global challenges of the 21st century, with far-reaching implications for various sectors, including agriculture. This thesis aims to investigate the multifaceted impacts of climate change on global agricultural systems, focusing on changes in crop yields, water availability, and pest dynamics. The research is motivated by the urgent need to understand and address the challenges posed by climate change to ensure food security and sustainable agricultural practices worldwide."
draw_wrapped_text(c, intro_text, margin_left, margin_top - line_height, max_width)
c.showPage()
# Save the PDF
c.save()
print(f"PDF created successfully: {file_name}")
I then adjusted the max_width section to 7*inches and got this:
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
# Create a PDF file
file_name = "example_thesis_reportlab.pdf"
c = canvas.Canvas(file_name, pagesize=letter)
# Define margin and width
margin_left = 50
margin_top = 750
max_width = 7 * inch
line_height = 20
# Function to wrap text
def wrap_text(text, max_width, font_name, font_size):
words = text.split()
lines = []
current_line = []
for word in words:
test_line = ' '.join(current_line + [word])
width = pdfmetrics.stringWidth(test_line, font_name, font_size)
if width <= max_width:
current_line.append(word)
else:
lines.append(' '.join(current_line))
current_line = [word]
if current_line:
lines.append(' '.join(current_line))
return lines
# Function to draw wrapped text
def draw_wrapped_text(c, text, x, y, max_width, font_name="Helvetica", font_size=12):
c.setFont(font_name, font_size)
lines = wrap_text(text, max_width, font_name, font_size)
for line in lines:
c.drawString(x, y, line)
y -= line_height
return y # Return the new y position
# Title Page
c.setFont("Helvetica-Bold", 16)
c.drawString(margin_left, margin_top, "Title: The Impact of Climate Change on Global Agriculture")
c.setFont("Helvetica", 12)
c.drawString(margin_left, margin_top - line_height, "Author: John Doe")
c.drawString(margin_left, margin_top - 2 * line_height, "Date: September 9, 2024")
c.showPage()
# Abstract
c.setFont("Helvetica-Bold", 14)
c.drawString(margin_left, margin_top, "Abstract")
abstract_text = "This thesis explores the various impacts of climate change on global agriculture, including changes in crop yields, water availability, and pest dynamics. The research synthesizes data from multiple sources to provide a comprehensive analysis of the challenges faced by the agricultural sector in the context of a changing climate. The findings suggest significant regional variations in the effects of climate change on agriculture, with some areas experiencing increased productivity while others face severe challenges. This study aims to contribute to the development of adaptive strategies for sustainable agriculture in the face of climate change."
draw_wrapped_text(c, abstract_text, margin_left, margin_top - line_height, max_width)
c.showPage()
# Table of Contents
c.setFont("Helvetica-Bold", 14)
c.drawString(margin_left, margin_top, "Table of Contents")
toc_items = [
"1. Introduction",
"2. Literature Review",
"3. Methodology",
"4. Results",
"5. Discussion",
"6. Conclusion",
"7. References"
]
for i, item in enumerate(toc_items):
c.setFont("Helvetica", 12)
c.drawString(margin_left, margin_top - (i + 1) * line_height, item)
c.showPage()
# Introduction
c.setFont("Helvetica-Bold", 14)
c.drawString(margin_left, margin_top, "1. Introduction")
intro_text = "Climate change has emerged as one of the most pressing global challenges of the 21st century, with far-reaching implications for various sectors, including agriculture. This thesis aims to investigate the multifaceted impacts of climate change on global agricultural systems, focusing on changes in crop yields, water availability, and pest dynamics. The research is motivated by the urgent need to understand and address the challenges posed by climate change to ensure food security and sustainable agricultural practices worldwide."
draw_wrapped_text(c, intro_text, margin_left, margin_top - line_height, max_width)
c.showPage()
# Save the PDF
c.save()
print(f"PDF created successfully: {file_name}")
But I still want to play around with the function to see what I can achieve.