I am trying to produce a PDF document with different odd/even page layouts (to allow asymmetric borders for binding) using Python 2.7 and ReportLab. To further complicate matters I am trying to produce two-columns per page.
def WritePDF(files):
story = []
doc = BaseDocTemplate("Polar.pdf", pagesize=A4, title = "Polar Document 5th Edition")
oddf1 = Frame(doc.leftMargin, doc.bottomMargin, doc.width/2-6, doc.height, id='oddcol1')
oddf2 = Frame(doc.leftMargin+doc.width/2+6, doc.bottomMargin, doc.width/2-6, doc.height, id='oddcol2')
evenf1 = Frame(doc.leftMargin, doc.bottomMargin, doc.width/2-6, doc.height, id='evencol1')
evenf2 = Frame(doc.leftMargin+doc.width/2+6, doc.bottomMargin, doc.width/2-6, doc.height, id='evencol2')
doc.addPageTemplates([PageTemplate(id='EvenTwoC',frames=[evenf1,evenf2],onPage=evenFooter),
PageTemplate(id='OddTwoC', frames=[oddf1, oddf2], onPage=oddFooter)])
...
story.append(Paragraph(whatever, style))
What I can't figure out is how to make ReportLab alternate between right and left (or odd and even) pages. Any suggestions?
I found the solution I guess ! :)
I had to dig into the source code. I found the solution in the file reportlab/platypus/doctemplate.py
on line 636. It's not the first time I had to do this, as the documentation is pretty limited...
Now, what I found:
def handle_nextPageTemplate(self,pt):
'''On endPage change to the page template with name or index pt'''
if type(pt) is StringType:
# ... in short, set self._nextPageTemplate
elif type(pt) is IntType:
# ... in short, set self._nextPageTemplate
elif type(pt) in (ListType, TupleType):
#used for alternating left/right pages
#collect the refs to the template objects, complain if any are bad
c = PTCycle()
for ptn in pt:
found = 0
if ptn=='*': #special case name used to short circuit the iteration
c._restart = len(c)
continue
for t in self.pageTemplates:
if t.id == ptn:
c.append(t)
found = 1
if not found:
raise ValueError("Cannot find page template called %s" % ptn)
if not c:
raise ValueError("No valid page templates in cycle")
elif c._restart>len(c):
raise ValueError("Invalid cycle restart position")
#ensure we start on the first one
self._nextPageTemplateCycle = c.cyclicIterator()
else:
raise TypeError("argument pt should be string or integer or list")
And I checked where this self._nextPageTemplateCycle
is used, so this is what I think should work (not tested though):
story = []
# ...
# doc.addPageTemplates([...])
story.append(NextPageTemplate(['pageLeft', 'pageRight'])) # this will cycle through left/right/left/right/...
story.append(NextPageTemplate(['firstPage', 'secondPage', '*', 'pageLeft', 'pageRight'])) # this will cycle through first/second/left/right/left/right/...
Add this to story once, when you want to start alternating pages. Use another normal NextPageTemplate to stop this cycle (because in the source, there's a del self._nextPageTemplateCycle
if you do that).
Hope it helps, and do say if it works, I can't make sure right now, but I will!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With