Changeset 15880


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.

Location:
main/waeup.kofa/trunk
Files:
1 deleted
6 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/CHANGES.txt

    r15875 r15880  
    441.6.1.dev0 (unreleased)
    55=======================
     6
     7* Add LetterPDFCreator utility and use this pdf creator
     8  for admission slips. letterhead_admission.jpg file
     9  must exist in students/static.
    610
    711* Add application worklfow transition `reset6`.
  • 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
  • main/waeup.kofa/trunk/src/waeup/kofa/students/browser.py

    r15865 r15880  
    2222import pytz
    2323import sys
     24import os
    2425import textwrap
    2526from cStringIO import StringIO
     
    543544    def render(self):
    544545        students_utils = getUtility(IStudentsUtils)
     546        letterhead_path = os.path.join(
     547            os.path.dirname(__file__), 'static', 'letterhead_admission.jpg')
     548        if not os.path.exists(letterhead_path):
     549            letterhead_path = None
    545550        return students_utils.renderPDFAdmissionLetter(self,
    546             self.context.student, omit_fields=self.omit_fields)
     551            self.context.student, omit_fields=self.omit_fields,
     552            letterhead_path=letterhead_path)
    547553
    548554class StudentBaseManageFormPage(KofaEditFormPage):
  • main/waeup.kofa/trunk/src/waeup/kofa/students/fileviewlets.py

    r15833 r15880  
    2424    IExtFileStore, IFileStoreNameChooser, IKofaObject, IKofaUtils)
    2525from waeup.kofa.utils.helpers import string_from_bytes, file_size
    26 from waeup.kofa.browser import DEFAULT_IMAGE_PATH
    2726
    2827from waeup.kofa.students.interfaces import IStudent, IStudentsUtils
  • main/waeup.kofa/trunk/src/waeup/kofa/students/interfaces.py

    r15790 r15880  
    145145
    146146    def renderPDFAdmissionLetter(view, student=None, omit_fields=(),
    147                                  pre_text=None, post_text=None,):
     147                                 pre_text=None, post_text=None,
     148                                 letterhead_path=None):
    148149        """Render pdf admission letter.
    149150        """
  • main/waeup.kofa/trunk/src/waeup/kofa/students/utils.py

    r15879 r15880  
    859859
    860860    def renderPDFAdmissionLetter(self, view, student=None, omit_fields=(),
    861                                  pre_text=None, post_text=None,):
     861                                 pre_text=None, post_text=None,
     862                                 letterhead_path=None):
    862863        """Render pdf admission letter.
    863864        """
     
    865866            return
    866867        style = getSampleStyleSheet()
    867         creator = self.getPDFCreator(student)
     868        if letterhead_path:
     869            creator = getUtility(IPDFCreator, name='letter')
     870        else:
     871            creator = getUtility(IPDFCreator)
    868872        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
    869873        data = []
     
    912916        pdf_stream = creator.create_pdf(
    913917            data, None, doc_title, author=author, footer=footer_text,
    914             note=post_text)
     918            note=post_text, letterhead_path=letterhead_path)
    915919        return pdf_stream
    916920
Note: See TracChangeset for help on using the changeset viewer.