我正在尝试使用Python2.7和ReportLab生成一个具有不同奇偶页面布局的PDF文档(以允许非对称边框进行绑定)。为了使问题更加复杂,我正在尝试每页生成两列。
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))
我不明白的是如何让ReportLab在左右(或者奇数和偶数)页面之间切换。有什么建议吗?
发布于 2012-06-19 00:43:33
我想我找到了解决方案!:)
我不得不深入研究源代码。我在文件reportlab/platypus/doctemplate.py
的636行找到了解决方案。这不是我第一次这样做了,因为文档非常有限……
现在,我发现了:
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")
我检查了这个self._nextPageTemplateCycle
的使用位置,所以这是我认为应该可以工作的(虽然没有测试):
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/...
当您想要开始交替页面时,只需将此内容添加到故事中一次。使用另一个普通的NextPageTemplate来停止这个循环(因为在源代码中,如果您这样做,就会有一个del self._nextPageTemplateCycle
)。
希望它能帮上忙,如果它能工作,我现在还不能确定,但我会的!
https://stackoverflow.com/questions/11042327
复制相似问题