Search code examples
reportlab

reportlab LayoutError: Flowable Spacer too large


I'm using reportlab 3.2.0.

SPACER = Spacer(0, 10)
buff = BytesIO()
doc = SimpleDocTemplate(buff, rightMargin=0.2 * inch,
                        leftMargin=0.2 * inch,\
                        topMargin=100, bottomMargin=68)
elements = []
# here add elements
elements.append(SPACER)
# here add more elements
doc.build(elements)

Whenever the space ends up close to the bottom of the page, I get this error:

File "/venv/lib/python2.7/site-packages/reportlab/platypus/doctemplate.py", line 1171, in build
    BaseDocTemplate.build(self,flowables, canvasmaker=canvasmaker)
  File "/venv/lib/python2.7/site-packages/reportlab/platypus/doctemplate.py", line 927, in build
    self.handle_flowable(flowables)
  File "/venv/lib/python2.7/site-packages/reportlab/platypus/doctemplate.py", line 829, in handle_flowable
    raise LayoutError(ident)
LayoutError: Flowable <Spacer at 0x4c87d40 frame=normal>...(0 x 10) too large on page 2 in frame 'normal'(554.475590551 x 661.88976378) of template 'Later'

Is there a way to prevent it from throwing an exception (i.e. just ignore the space element)?


Solution

  • Here's a quick implementation of Spacer that uses up either the smaller of the height you assigned to it or the space left on the current page.

    class ConditionalSpacer(Spacer):
    
        def wrap(self, availWidth, availHeight):
            height = min(self.height, availHeight-1e-8)
            return (availWidth, height)
    

    You can use it just like Spacer.