问题
How to determine on what page(need a page number) will be each flowable after rendering to pdf. I was thinking to add a custom id attribute to flowable, so i will know what flowable is it. But how can i determine on what page it will be placed? What is the best way to achieve this?
回答1:
At what point do you need this information? It becomes available as the document is constructed, so you can get it after rendering by overriding methods such as afterPage
, afterDrawPage
, and afterFlowable
. You can then get the page number from the DocTemplate
class (I believe there's a class variable called something like _currentPage
, but you'll need to check the ReportLab code since I don't think it's documented).
回答2:
I ended with following solution. Addeda an custom id flo_id to every flowable. And override method handle_flowable in BaseDocTemplate, where was checking and saving id,
class SignDocTemplate(BaseDocTemplate):
blocks_to_pages = {}
def handle_flowable(self, flowables):
f = flowables[0]
BaseDocTemplate.handle_flowable(self, flowables)
if hasattr(f,'flo_id'):
if self.blocks_to_pages.has_key(self.canv._pageNumber):
self.blocks_to_pages[self.canv._pageNumber].append(f.flo_id)
else:
self.blocks_to_pages[self.canv._pageNumber]= [f.flo_id,]
And after building a doc it will be available at document instance in blocks_to_pages variable.
来源:https://stackoverflow.com/questions/9045937/how-to-know-on-what-page-number-flowable-was-placed-during-rendering-a-pdf-with