Search code examples
pythonhtmlreportlab

How do I add breaklines in reportlab PDF in HTML format?


I am trying to put some data in PDF with reportlab. I fetch this data from a dataframe. However, since one field is a little text, this should have some breaklines to make it readable in the PDF.

The part of the code where I extract my data:

  style_texto4=ParagraphStyle(name='Normal4', fontName='Times-Roman', fontSize=15)
  texto4=Paragraph(f"{df.set_index('concat').loc[i]['Text']}", style=style_texto4)
  style_texto4.leading=16
  texto4.wrapOn(c, 600, 600)
  texto4.drawOn(c, 90, 380)

Imagine my text is like this:

'Hello, my name is John and I am trying to insert some breaklines \n in my pdf so that it displays correctly in my html format'.

And this raises:

syntax error: No content allowed in br tag

Solution

  • Newline character (\n) will not work. You can achieve this by using <br /> tag.

    Help: How to insert a carriage return in a ReportLab paragraph?