Ignore:
Timestamp:
11 Dec 2019, 12:02:09 (5 years ago)
Author:
Henrik Bettermann
Message:

Add LetterPDFCreator utility and use this pdf creator
for admission slips. letterhead_admission.jpg file
must exist in students/static.

File:
1 edited

Legend:

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

    r15834 r15880  
    802802        if not getattr(doc, 'kofa_nodate', False):
    803803            tz = getattr(queryUtility(IKofaUtils), 'tzinfo', pytz.utc)
    804             #tz = getUtility(IKofaUtils).tzinfo
    805804            today = now(tz).strftime('%d/%m/%Y %H:%M:%S %Z')
    806805            canvas.drawString(2.2*cm, 0.5 * inch,
     
    811810        canvas.kofa_footer_text =  doc.kofa_footer
    812811        canvas.restoreState()
    813         canvas.restoreState()
    814812
    815813        # Metadata
     
    820818
    821819    def create_pdf(self, data, headerline=None, title=None, author=None,
    822                    footer='', note=None, sigs_in_footer=[], topMargin=1.5):
     820                   footer='', note=None, sigs_in_footer=[], topMargin=1.5,
     821                   letterhead_path=None):
    823822        """Returns a binary data stream which is a PDF document.
    824823        """
    825824        pdf_stream = StringIO()
    826825        bottomMargin = len(sigs_in_footer) and 1.9*inch or 1.2*inch
    827         topMargin += title.count('\n') * 0.2
     826        if letterhead_path:
     827            topMargin += 1.2
     828        else:
     829            topMargin += title.count('\n') * 0.2
    828830        doc = SimpleDocTemplate(
    829831            pdf_stream,
     
    841843        doc.kofa_footer = footer
    842844        doc.sigs_in_footer = sigs_in_footer
     845        doc.letterhead_path = letterhead_path
    843846        if note is not None:
    844847            html = format_html(note)
     
    859862    pagesize = landscape(A4)
    860863
     864class LetterPDFCreator(PDFCreator):
     865    """A utility to help with generating PDF docs with an original letterhead.
     866    """
     867    grok.name('letter')
     868    letterhead_pos = [0, 0]
     869
     870    def paint_background(self, canvas, doc):
     871        """Paint letterhead background of a PDF.
     872
     873        The `doc` is expected to be some reportlab SimpleDocTemplate
     874        or similar object.
     875
     876        This is a callback method that will be called from reportlab
     877        when creating PDFs with :meth:`create_pdf`.
     878        """
     879        canvas.saveState()
     880        width, height = doc.width, doc.height
     881        width += doc.leftMargin + doc.rightMargin
     882        height += doc.topMargin + doc.bottomMargin
     883
     884        # Letterhead
     885        if doc.letterhead_path is not None:
     886            canvas.saveState()
     887            canvas.drawImage(doc.letterhead_path,
     888                self.letterhead_pos[0], self.letterhead_pos[1],
     889                width=width, height=height)
     890            canvas.restoreState()
     891
     892        # Footer
     893        if getattr(doc, 'sigs_in_footer', False):
     894            self._drawSignatureBoxes(
     895                canvas, width, height, doc.sigs_in_footer)
     896        #canvas.line(2.2*cm, 0.62*inch, width-2.2*cm, 0.62*inch)
     897        canvas.setFont("Helvetica", 9)
     898        if not getattr(doc, 'kofa_nodate', False):
     899            tz = getattr(queryUtility(IKofaUtils), 'tzinfo', pytz.utc)
     900            today = now(tz).strftime('%d/%m/%Y %H:%M:%S %Z')
     901            #canvas.drawString(2.2*cm, 0.5 * inch,
     902            #    translate(_(u'Date: ${a}', mapping = {'a': today})))
     903            canvas.drawString(2.2*cm, 0.3 * inch,
     904                translate(_(u'Date: ${a}', mapping = {'a': today})))
     905        # set canves attributes needed to render `page x of y`
     906        canvas.kofa_footer_x_pos = width-2.2*cm
     907        #canvas.kofa_footer_y_pos = 0.5 * inch
     908        canvas.kofa_footer_y_pos = 0.3 * inch
     909        canvas.kofa_footer_text =  doc.kofa_footer
     910        canvas.restoreState()
     911
     912        # Metadata
     913        title = getattr(doc, 'kofa_title', '')
     914        canvas.setAuthor(getattr(doc, 'kofa_author', 'Unknown'))
     915        canvas.setSubject(title)
     916        canvas.setCreator(u'WAeUP Kofa')
     917        return
     918
     919
    861920class A3LandscapePDFCreator(LandscapePDFCreator):
    862921    """A utility to help with generating PDF docs in
Note: See TracChangeset for help on using the changeset viewer.