- Timestamp:
- 11 Apr 2012, 21:29:17 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.kofa/trunk/src/waeup/kofa/browser/pdf.py
r8111 r8113 23 23 from cStringIO import StringIO 24 24 from datetime import datetime 25 from reportlab.lib.units import cm, inch 25 from reportlab.lib.units import cm, inch, mm 26 26 from reportlab.lib.pagesizes import A4, landscape, portrait 27 27 from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle 28 from reportlab.pdfgen.canvas import Canvas 28 29 from reportlab.platypus import ( 29 30 SimpleDocTemplate, Spacer, Paragraph, Image, Table) … … 80 81 html = html.replace('\n', '') 81 82 return html 83 84 class NumberedCanvas(Canvas): 85 """A reportlab canvas for numbering pages after all docs are processed. 86 87 Taken from 88 http://code.activestate.com/recipes/546511-page-x-of-y-with-reportlab/ 89 http://code.activestate.com/recipes/576832/ 90 """ 91 92 def __init__(self, *args, **kw): 93 Canvas.__init__(self, *args, **kw) 94 self._saved_page_states = [] 95 return 96 97 def showPage(self): 98 self._saved_page_states.append(dict(self.__dict__)) 99 self._startPage() 100 return 101 102 def save(self): 103 """add page info to each page (page x of y)""" 104 num_pages = len(self._saved_page_states) 105 for state in self._saved_page_states: 106 self.__dict__.update(state) 107 self.draw_page_number(num_pages) 108 Canvas.showPage(self) 109 Canvas.save(self) 110 return 111 112 def draw_page_number(self, page_count): 113 """draw string at bottom right with 'page x of y'. 114 115 Location of the string is determined by canvas attributes 116 `kofa_footer_x_pos` and `kofa_footer_y_pos` that have to be 117 set manually. 118 119 If this canvas also provides an attribute `kofa_footer_text`, 120 the contained text is rendered left of the ``page x of y`` 121 string. 122 """ 123 self.setFont("Helvetica", 9) 124 right_footer_text = _( 125 '${footer_text} Page ${num1} of ${num2}', 126 mapping = {'footer_text': self.kofa_footer_text, 127 'num1':self._pageNumber, 'num2':page_count}) 128 self.drawRightString( 129 self.kofa_footer_x_pos, self.kofa_footer_y_pos, 130 translate(right_footer_text)) 131 return 82 132 83 133 class PDFCreator(grok.GlobalUtility): … … 281 331 canvas.drawString(2.2*cm, 0.5 * inch, 282 332 translate(_(u'Date: ${a}', mapping = {'a': today}))) 283 canvas.drawRightString(284 width-2.2*cm, 0.5 * inch,285 translate(_(u'${b} Page ${a}' , mapping = {'a':doc.page,286 'b':doc.kofa_footer})))333 # set canves attributes needed to render `page x of y` 334 canvas.kofa_footer_x_pos = width-2.2*cm 335 canvas.kofa_footer_y_pos = 0.5 * inch 336 canvas.kofa_footer_text = doc.kofa_footer 287 337 canvas.restoreState() 288 338 canvas.restoreState() … … 314 364 doc.kofa_footer = footer 315 365 doc.build(data, onFirstPage=self.paint_background, 316 onLaterPages=self.paint_background) 366 onLaterPages=self.paint_background, 367 canvasmaker=NumberedCanvas 368 ) 317 369 result = pdf_stream.getvalue() 318 370 pdf_stream.close()
Note: See TracChangeset for help on using the changeset viewer.