Changeset 8113


Ignore:
Timestamp:
11 Apr 2012, 21:29:17 (12 years ago)
Author:
uli
Message:

Seem I found an efficient solution for the 'page x of y' problem.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/browser/pdf.py

    r8111 r8113  
    2323from cStringIO import StringIO
    2424from datetime import datetime
    25 from reportlab.lib.units import cm, inch
     25from reportlab.lib.units import cm, inch, mm
    2626from reportlab.lib.pagesizes import A4, landscape, portrait
    2727from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
     28from reportlab.pdfgen.canvas import Canvas
    2829from reportlab.platypus import (
    2930    SimpleDocTemplate, Spacer, Paragraph, Image, Table)
     
    8081    html = html.replace('\n', '')
    8182    return html
     83
     84class 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
    82132
    83133class PDFCreator(grok.GlobalUtility):
     
    281331            canvas.drawString(2.2*cm, 0.5 * inch,
    282332                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
    287337        canvas.restoreState()
    288338        canvas.restoreState()
     
    314364        doc.kofa_footer = footer
    315365        doc.build(data, onFirstPage=self.paint_background,
    316                   onLaterPages=self.paint_background)
     366                  onLaterPages=self.paint_background,
     367                  canvasmaker=NumberedCanvas
     368                  )
    317369        result = pdf_stream.getvalue()
    318370        pdf_stream.close()
Note: See TracChangeset for help on using the changeset viewer.