Search code examples
pythonrowreportlab

Python Reportlab RML. How to split table row on two pages


I'm wondering if there is some functionality to split table row on two and more pages. Cause some information in row can be too long for one page, and if one row longer then page size then it's cause an exception.


Solution

  • ReportLab does not text wrapping out of the box, so I am assuming you are either using a para in the table cells, or you are breaking your lines manually with simpleSplit.

    If you text is one line string then you can use

    from reportlab.pdfbase.pdfmetrics import stringWidth
    textWidth = stringWidth(text, fontName, fontSize)
    

    If your text was multi-lines, assuming you are working in a rectangular area with defined width, then do

    from reportlab.lib.utils import simpleSplit
    lines = simpleSplit(text, fontName, fontSize, maxWidth)
    

    lines is a list of all the lines of your paragraph, if you know the line spacing value then the height of the paragraph can be calculated as lineSpacing*len(lines)

    If this proved to be longer than your page then using whatever template you are in (preppy, django, ninja, etc) finds a good breaking point for your text and ends the current row and starts a new one.

    I hope this helps

    Meitham

    p.s. you could always send your questions to the reportlab mailing list and they usually very quick in answering these questions.