This works to write a text in a PDF file with reportlab
:
from reportlab.pdfgen import canvas
from reportlab.lib.units import cm
c = canvas.Canvas("test.pdf")
c.drawString(1 * cm, 29.7 * cm - 1 * cm, "Hello")
c.save()
but when dealing with multiple lines of text, it's unpleasant to have to handle the x, y
coordinate of each new line:
text = "Hello\nThis is a multiline text\nHere we have to handle line height manually\nAnd check that every line uses not more than pagewidth"
c = canvas.Canvas("test.pdf")
for i, line in enumerate(text.splitlines()):
c.drawString(1 * cm, 29.7 * cm - 1 * cm - i * cm, line)
c.save()
Is there a more clever way to do this with reportlab
?
` is not enough to do this; one seems to have to introduce `Paragraph` class, etc. so this question is different in the sense it asks if we have to introduce a layout system like `Paragraph` or if there are other options. – Basj May 25 '18 at 15:21