Ignore:
Timestamp:
9 Jul 2020, 13:25:01 (4 years ago)
Author:
Henrik Bettermann
Message:

Prepare renderPDFAdmissionLetter for merging with another pdf document.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/students/utils.py

    r16126 r16157  
    2323from time import time
    2424from cStringIO import StringIO
     25from PyPDF2 import PdfFileMerger, PdfFileReader, PdfFileWriter
    2526from reportlab.lib import colors
    2627from reportlab.lib.units import cm
     
    405406
    406407    def setPaymentDetails(self, category, student,
    407             previous_session, previous_level, combi=[]):
     408            previous_session=None, previous_level=None, combi=[]):
    408409        """Create a payment ticket and set the payment data of a
    409410        student for the payment category specified.
     
    878879                                 pre_text=None, post_text=None,
    879880                                 topMargin = 1.5,
    880                                  letterhead_path=None):
     881                                 letterhead_path=None,
     882                                 mergefiles=None, watermark=None):
    881883        """Render pdf admission letter.
    882884        """
     
    936938            note=post_text, topMargin=topMargin,
    937939            letterhead_path=letterhead_path)
     940        if mergefiles:
     941            merger = PdfFileMerger()
     942            merger.append(StringIO(pdf_stream))
     943            if watermark:
     944                watermark = PdfFileReader(watermark)
     945            for file in mergefiles:
     946                if watermark:
     947                    # Pass through all pages of each file
     948                    # and merge with watermark page.
     949                    marked_file = PdfFileWriter()
     950                    orig_file = PdfFileReader(file[1])
     951                    num_pages = orig_file.getNumPages()
     952                    for num in range(num_pages):
     953                        page = orig_file.getPage(num)
     954                        page.mergePage(watermark.getPage(0))
     955                        marked_file.addPage(page)
     956                    # Save into a file-like object
     957                    tmp1 = StringIO()
     958                    marked_file.write(tmp1)
     959                    # Append the file-like object
     960                    merger.append(tmp1)
     961                else:
     962                    # Just append the file object
     963                    merger.append(file[1])
     964            # Save into a file-like object
     965            tmp2 = StringIO()
     966            merger.write(tmp2)
     967            return tmp2.getvalue()
    938968        return pdf_stream
    939969
Note: See TracChangeset for help on using the changeset viewer.