How to know on what page number flowable was placed during rendering a pdf with reportlab

北慕城南 提交于 2019-12-08 03:00:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!