2013-06-12 3 views
2

송장을 작성 중이고 마지막 페이지에만 바닥 글을 추가하고 싶습니다 (첫 페이지 일 수도 있음).Reportlab의 마지막 데이터 페이지에만있는 바닥 글

테이블의 데이터가 동적이므로 페이지 수를 계산할 수 없습니다.

이제 첫 페이지 (2 프레임 및 footer1)와 다음 페이지 (1 프레임 및 footer2)의 2 페이지 템플릿으로 작업하고 있습니다. 이 작업은 데이터가 두 페이지로 채워지지만 테이블이 1 페이지 또는 2 페이지 만 채우면 더 이상 작동하지 않습니다.

나는 다음과 같은 방법으로 바닥 글 정의 :

footerFrame = Frame(x1=35*mm, y1=20*mm, width=175*mm, height=15*mm) 
    footerStory = [ Paragraph("Have a nice day.", styles["fancy"]) ] 

    def footer2(canvas,document): 
     canvas.saveState() 
     footerFrame.addFromList(footerStory, canvas) 
     canvas.restoreState() 

바닥 글을 정의하는보다 유연한 방법이 있나요를, 그래서 그것은 단지 테이블이 종료되는 페이지에 표시?

미리 감사드립니다.

답변

3

ReportLabs 캔버스 클래스를 재정 의하여 페이지를 추적 할 수 있습니다 (유동성을 포함하지 않는 다른 보고서를 사용하여 부여했지만, 여전히 작동 할 수 있다고 생각합니다).

유동적 (단락)을 사용하고 있으므로 새로운 PDF 생성마다 길이가 동적 인 부분이 몇 페이지인지 알아야합니다. 나는 100 % 긍정적이지는 않지만 ReportLab의 유동성은 여전히 ​​캔버스의 "showPage()"메소드를 호출한다고 생각합니다.

의사 코드/부품 - 파이썬에서

, 나는 다음과 같은 (테스트되지 않은)를 권 해드립니다 : 그래서 당신은이 다음과 같은 작업을 수행 할 수

class MyFooterCanvas(canvas.Canvas): 
    def __init__(self, *args, **kwargs): 
     ## Subclass the ReportLab canvas class. 
     canvas.Canvas.__init__(self, *args, **kwargs) 
     ## Create an empty list to store the saved pages so you can keep track of them. 
     self._savedPages = [] 

    def showPage(self): 
     """Override the showPage method""" 

     ## We are writing our own showPage() method here, so we can keep track of 
     ## what page we are on. 
     self._savedPages.append(self) 
     ## Start a new page. 
     self._startPage() 

    def drawFooter(self): 
     """Draws the footer to the page. You can have it do whatever you want here""" 
     self.drawString(50, 50, "This is my footer, yay me - footer freedom") 

    def save(self): 
     """Saves the entire PDF in one go, a bit costly to do it this way, 
      but certainly one way to get a footer.""" 
     numPages = len(self._savedPages) 

     for pages in self._savedPages: 
      ## Finds the last page and when it 'hits', it will call the self.drawFooter() method. 
      if pages == self._savedPages[-1]: 
       self.drawFooter() 
      else: 
       ## If it's not the last page, explicitly pass over it. Just being thorough here. 
       pass 

     ## And... continue doing whatever canvas.Canvas() normally does when it saves. 
     canvas.Canvas.save(self) 

을 다시을이 검증되지 않은,하지만 난이 줄 것이라고 생각 당신이 원하는 행동. 그것을 시도해라, 만일 당신이 붙이게되면, 내가 필요로하는, 이것의 일부를 해싱 할 수있다. 그러나 나는 다른 non-flowables를 위해이 같은 방법을했고 그것은 과거에 나를 위해 일했다.

+0

감사합니다. 시험해보고 알려 드리겠습니다. – Helga

+0

귀하의 사례도 저에게 도움이되었습니다. 감사합니다. init__ 다음에 두 번째 줄에 작은 따옴표가 있습니다. – Helga

+0

@Helga, 다행스럽게도 당신을 위해 일했습니다! 그 작은 오타를 찾아 주셔서 고마워요. 지금 고쳐야합니다. 또한,이 질문을 올바른 것으로 표시하는 것이 너무 친절하다면! 나는 '점수'를 얻는 것을 좋아한다 : P * 자기 홍보 한 번에 한 가지 질문! * –