Search code examples
pythonreportlabword-wrap

python wrap text and reportlab


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!


Solution

  • 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