Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Word wrap on report lab PDF table

I'm using the Table of Report Lab library to print a table on a PDF report. I would like to know if it's possible to configure the table to perform an automatic wrapping of the content of a cell.

For example, I have a text that doesn't fit on a cell inside a column. I would like that the table performs the wrap automatically adjusting the content of the cells to fit on the columns width. Is it possible?

like image 579
Pedro Ghilardi Avatar asked Jan 23 '10 03:01

Pedro Ghilardi


2 Answers

You can put any flowable in a table element. It is probably good practice to have all table elements as flowables, so they can be styled the same. For your case you will most likely need a Paragraph flowable. eg.

styles = getSampleStyleSheet()
text = Paragraph("long line",
              styles['Normal'])

You can put `text' into the data you feed to a table and it will automatically wrap.

like image 182
David Raznick Avatar answered Sep 19 '22 15:09

David Raznick


My solution, force newline in the string:

def __chopLine(line, maxline):

    cant = len(line) / maxline
    cant += 1
    strline = ""
    index = maxline
    for i in range(1,cant):
        index = maxline * i
        strline += "%s\n" %(line[(index-maxline):index])
    strline += "%s\n" %(line[index:])
    return strline
like image 31
pako anguiano Avatar answered Sep 19 '22 15:09

pako anguiano