Reportlab : 如何在纵向和横向之间切换? [英] Reportlab : How to switch between portrait and landscape?
问题描述
我正在使用 reportlab 从动态数据中自动生成 pdf 报告.由于内容有时太大而无法纵向显示,我尝试将大内容切换为横向.
I am using reportlab to generate a pdf report automatically from dynamic data. As the content sometimes is too large to be displayed in portrait, I am trying to switch to landscape for large content.
这是我的报告生成的工作原理:
Here is how my report generation works :
主要功能:
doc = DocTemplate(...) //Doctemplate is a customed BaseDocTemplate class
array = []
some_data= "Here is some data displayed in portrait"
array.append(Paragraph(some_data))
large_data = "this data is too large to be displayed in portrait"
array.append(Paragraph(large_data))
... // Some more data is added after this
doc.build(array, canvasmaker=NumberedCanvas)
我正在寻找一种能够在每一步从纵向切换到横向的方法,因为我不知道显示它所需的页数.我仍然是 reportlab 的新手,甚至有点使用 python,所以我不知道如何在最后构建整个文档时正确使用 reportlab(PageTemplates,flowables)提供的解决方案.
What I am looking for is a way to be able to switch from portrait to landscape at each step, as I don't know the number of pages that will be needed to display it. I am still new to reportlab and even a bit with python, so I do not see how I can use the solutions provided by reportlab (PageTemplates, flowables) properly as I am building the whole document at the end.
对于这种情况,这是我的其他有用类:
Here are my other useful classes for this case :
class DocTemplate(BaseDocTemplate, ):
def __init__(self, filename, **kw):
apply(BaseDocTemplate.__init__, (self, filename), kw)
f = Frame(2.6*cm, 2.8*cm, 16*cm, 22.7*cm, id='f')
pt = PageTemplate('RectPage', [f], onPage=beforeDrawPage, onPageEnd=afterDrawPage)
//beforeDrawPage and afterDrawPage fill the headers of the page (that also need to be in landscape)
self.addPageTemplates(pt)
我想我应该添加另一个页面模板或框架,但我不知道如何在数据附加阶段从一个页面切换到另一个页面.
I think I shall add another page template or frame, but I don't see how i can switch from one to the other during the data appending phase.
class NumberedCanvas(canvas.Canvas):
def __init__(self, *args, **kwargs):
canvas.Canvas.__init__(self, *args, **kwargs)
self._saved_page_states = []
def showPage(self):
self._saved_page_states.append(dict(self.__dict__))
self._startPage()
def save(self):
"""add page info to each page (page x of y)"""
num_pages = len(self._saved_page_states)
for state in self._saved_page_states:
self.__dict__.update(state)
self.draw_page_number(num_pages)
canvas.Canvas.showPage(self)
self.setTitle("Title")
canvas.Canvas.save(self)
self._doc.SaveToFile(self._filename, self)
def draw_page_number(self, page_count):
self.setFont("Helvetica", 11)
self.drawRightString(18.5*cm, 26.8*cm,
"PAGE %d / %d" % (self._pageNumber, page_count))
我希望我没有忘记任何需要澄清的事情.
I hope I did'nt forgot anything to be clear.
非常感谢.
推荐答案
我终于找到了最好的自己做的方法:
I finally figured out the best way to do it by myself :
我在我的 DocTemplate 中添加了一个具有横向设置的新 PageTemplate,然后简单地使用了 reportlab.platypus 包中的 NextPageTemplate :
I added a new PageTemplate in my DocTemplate with landscape settings, and then simply used NextPageTemplate from the reportlab.platypus package :
array.append(NextPageTemplate('landscape'))
要恢复纵向,我使用:
array.append(NextPageTemplate('portrait'))
这提供了非常好的灵活性.
This allows a pretty nice flexibility.
这篇关于Reportlab : 如何在纵向和横向之间切换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!