Created
March 20, 2013 10:11
-
-
Save KanorUbu/5203608 to your computer and use it in GitHub Desktop.
Display the total number of page with reportlab (source http://code.activestate.com/recipes/546511-page-x-of-y-with-reportlab/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NumberedCanvas(canvas.Canvas): | |
def __init__(self, *args, **kwargs): | |
# kwargs['verbosity'] = 1 | |
canvas.Canvas.__init__(self, *args, **kwargs) | |
self._codes = [] | |
def showPage(self): | |
_formsinuse = copy.deepcopy(self._formsinuse) | |
self._codes.append({'code': self._code, 'stack': self._codeStack, | |
'cM' : self._cropMarks, | |
'_pageRotation':self._pageRotation, | |
'_currentPageHasImages': self._currentPageHasImages, | |
'_pageTransition': self._pageTransition, | |
'_pageCompression': self._pageCompression, | |
'_pageDuration': self._pageDuration, | |
'_psCommandsBeforePage': self._psCommandsBeforePage, | |
'_preamble': self._preamble, | |
'_psCommandsAfterPage': self._psCommandsAfterPage, | |
'_onPage': self._onPage, | |
'_formsinuse': _formsinuse | |
}) | |
self._startPage() | |
def save(self): | |
"""add page info to each page (page x of y)""" | |
# reset page counter | |
self._pageNumber = 0 | |
for code in self._codes: | |
# recall saved page | |
self._code = code['code'] | |
self._codeStack = code['stack'] | |
self._cropMarks = code['cM'] | |
self._pageRotation = code['_pageRotation'] | |
self._currentPageHasImages = code['_currentPageHasImages'] | |
self._pageTransition = code['_pageTransition'] | |
self._pageCompression = code['_pageCompression'] | |
self._pageDuration = code['_pageDuration'] | |
self._psCommandsBeforePage = code['_psCommandsBeforePage'] | |
self._preamble = code['_preamble'] | |
self._psCommandsAfterPage = code['_psCommandsAfterPage'] | |
self._onPage = code['_onPage'] | |
self._formsinuse = code['_formsinuse'] | |
self.setFont("Helvetica", 7) | |
self.drawRightString(200*mm, 15*mm, | |
"Page %(this)i/%(total)i" % { | |
'this': self._pageNumber+1, | |
'total': len(self._codes), | |
} | |
) | |
canvas.Canvas.showPage(self) | |
self._doc.SaveToFile(self._filename, self) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment