Один из вариантов - использовать Flowables, которые предоставляет reportlab, один тип плавающего элемента - Paragraph
.Абзацы поддерживают <br>
как разрывы строк.
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4
from reportlab.platypus import SimpleDocTemplate, Paragraph
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.units import cm
my_text = "Hello\nThis is a multiline text\nHere we do not have to handle the positioning of each line manually"
doc = SimpleDocTemplate("example_flowable.pdf",pagesize=A4,
rightMargin=2*cm,leftMargin=2*cm,
topMargin=2*cm,bottomMargin=2*cm)
doc.build([Paragraph(my_text.replace("\n", "<br />"), getSampleStyleSheet()['Normal']),])
Второй вариант - использовать drawText
с TextObject
:
c = canvas.Canvas("test.pdf")
textobject = c.beginText(2*cm, 29.7 * cm - 2 * cm)
for line in my_text.splitlines(False):
textobject.textLine(line.rstrip())
c.drawText(textobject)
c.save()