0
votes

I have a little code and I would like to wrap my long string in every 10th character and then add it into a PDF using reportlab:

This is how I try:

text = '*long_text_long_text_long_text_long_text*'
text = "\n".join(wrap(text, 10))
canvas.drawString(5,227, text)

My pdf was created but where I want to break the lines I can only see black rectangles. You can see the attached picture:

enter image description here

Can you help me? Thank you!

1
I don't know what a canvas is, but I bet its drawString method does not natively support rendering multiline strings. You'll probably need to manually iterate through each line and draw them individually, specifying the Y-coordinates of each one using your own arithmetic. - Kevin

1 Answers

1
votes

drawString draws a single line. so you will need to adjust the coordinate for each line in a loop.

y = 227
for line in wrap(text, 10):
    canvas.drawString(5, y, line)
    y += 15