[reportlab-users] behaviour change from 2.5 to 3.0 (or 3.3)

Yitzchak Scott-Thoennes sthoenna at gmail.com
Wed Jul 6 11:39:03 EDT 2016


I'm upgrading a server from ubuntu 12.04 (with reportlab 2.5) to 14.04
(with reportlab 3.0) and seeing a difference I can't explain.  I'd
appreciate it if someone else could explain it.
I'd even more appreciate suggestions for improvement.

The following code results in "X  test" horizontally aligned in 2.5 but
puts the X a line below test in 3.0 or 3.3:

from reportlab.lib.pagesizes import letter
from reportlab.lib.styles import ParagraphStyle
from reportlab.platypus import Paragraph, SimpleDocTemplate
from reportlab.platypus.flowables import Spacer, Flowable

def main(file):
    baseStyle = ParagraphStyle('style', leftIndent = 5)

    story = [
        HorizFlowable([
            Cell('X', 10, baseStyle),
            Paragraph('text', baseStyle),
        ])
    ]

    doc = SimpleDocTemplate(
        file,
        pagesize = letter,
    )
    doc.build(list(story))

class Cell(Paragraph):
    """
    Flowable with a fixed width
    """
    __parent = Paragraph

    def __init__(self, text, width, *args, **kw):
        self.width = width
        self.__class__.__parent.__init__(self, text, *args, **kw)

    def wrap(self, aw, ah):
        return self.__class__.__parent.wrap(self, self.width, ah)

class HorizFlowable(Flowable):
    """
    Wrap a list of flowables and lays them out horizontally, sort of like
    an html table with only one row.
    """

    def __init__(self, content=None, *args, **kw):
        Flowable.__init__(self, *args, **kw)
        if not content:
            content = list()
        self._content = content

    def append(self, item):
        self._content.append(item)

    def wrap(self, availWidth, availHeight):
        height = 0
        colwidth = availWidth / len(self._content)
        width = availWidth
        for flowable in self._content:
            (w, h) = flowable.wrapOn(self.canv, width, availHeight)
            # you can't always have all the width
            if w == width and flowable != self._content[-1]:
                (w, h) = flowable.wrapOn(self.canv, colwidth, availHeight)
            width -= w
            if h > height:
                height = h
        self.width = availWidth - width
        self.height = height
        return self.width, self.height

    def draw(self):
        x = 0
        for flowable in self._content:
            flowable.drawOn(self.canv, x, self.height - flowable.height)
            x += flowable.width

main('output.pdf')
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://pairlist2.pair.net/pipermail/reportlab-users/attachments/20160706/9923f0bf/attachment.html>


More information about the reportlab-users mailing list