[reportlab-users] patch to do no PageBreak on an otherwise empty page + suggested refactoring of BaseDocTemplate.handle_pageBreak
Andy Robinson
andy at reportlab.com
Mon Sep 3 05:42:26 EDT 2012
Aha, I replied too soon to your earlier message before reading this - thanks.
This certainly makes sense on first reading. Robin Becker, the
principle maintainer of this stuff, has just gone on vacation for two
weeks. We'll try to give it a good test in the meantime and look at
the effects on a number of real world apps, and if it works we can
include it.
BTW we're aiming for an official release later in September so if
anyone else has patches or bugs to report, it's a good time...
- Andy
On 3 September 2012 09:22, Massa, Harald Armin <chef at ghum.de> wrote:
> Hello,
>
> the content of the pages within my application is dynamic. Certain
> section need to start on a new page.
>
> The obvious platypus solution is to:
>
> story.append(PageBreak())
> story.append(HeadOfNewSection())
>
> now it happens that the page right before the new section was filled
> up completely, and a new page has just begun.
> So the PageBreak() will insert an empty page - which is not wanted.
>
> CondPageBreak(height) does not work, as it is "a page break that only
> happens if there is less then (height) available".
>
> My solution: in the subclassing of BaseDocTemplate, I patched the
> handle_pageBreak as follows:
>
> def handle_pageBreak(self,slow=None):
> '''ignore page break on empty page'''
> if self._curPageFlowableCount == 0:
> return
>
> if self._pageBreakQuick and not slow:
> self.handle_pageEnd()
> else:
> n = len(self._hanging)
> while len(self._hanging)==n:
> self.handle_frameEnd()
>
>
> So, if handle_flowable is called with a PageBreak on an otherwise
> empty page (i.e. no flowable there), it is ignored.
>
> Which solves my problem so far, and is hereby published for others
> dealing with the same challenge.
>
> DRAWBACK: this will counteract to an empty page that is created on purpose.
>
> So my solution is the following refactoring of BaseDocTemplate:
>
> def handle_flowable(self,flowables):
> '''try to handle one flowable from the front of list flowables.'''
>
> #allow document a chance to look at, modify or ignore
> #the object(s) about to be processed
> self.filterFlowables(flowables)
>
> self.handle_breakBefore(flowables)
> self.handle_keepWithNext(flowables)
> f = flowables[0]
> del flowables[0]
> if f is None:
> return
>
> if isinstance(f,PageBreak):
> # pass the PageBreak object to the handle_pageBreak method
> self.handle_pageBreak(page_break_object=f)
> self.afterFlowable(f)
> elif isinstance(f,ActionFlowable):
> f.apply(self)
> self.afterFlowable(f)
> [...]
> #---------------------------
>
> def handle_pageBreak(self,slow=None, page_break_flowable=None):
> '''ignore page break on empty page'''
>
> if self._curPageFlowableCount == 0 and
> isinstance(page_break_flowable,PageBreakNotOnEmptyPage):
> return
>
> if self._pageBreakQuick and not isinstance(
> page_break_flowable ,SlowPageBreak):
> self.handle_pageEnd()
> else:
> n = len(self._hanging)
> while len(self._hanging)==n:
> self.handle_frameEnd()
>
> This allows to plug-in more various pageBreak-Handlers into the system
> without to much patching. The "slow"-parameter is kept, so existing
> code relying on it will continue to function.
>
> This patch is made available to integrate into reportlab or use by
> anyone/ if you need a code contribution agreement, please let me know.
>
> Best wishes,
>
> Harald
>
> --
>
> GHUM GmbH
> Harald Armin Massa
> Spielberger Straße 49
> 70435 Stuttgart
> 0173/9409607
>
> Amtsgericht Stuttgart, HRB 734971
> _______________________________________________
> reportlab-users mailing list
> reportlab-users at lists2.reportlab.com
> http://two.pairlist.net/mailman/listinfo/reportlab-users
--
Andy Robinson
Managing Director
ReportLab Europe Ltd.
Thornton House, Thornton Road, Wimbledon, London SW19 4NG, UK
Tel +44-20-8405-6420
More information about the reportlab-users
mailing list