Changeset 16253 for main


Ignore:
Timestamp:
29 Sep 2020, 08:26:52 (4 years ago)
Author:
Henrik Bettermann
Message:

Prepare renderPDF for merging with another pdf document.

File:
1 edited

Legend:

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

    r16159 r16253  
    772772        return getUtility(IPDFCreator)
    773773
     774    def _mergeFiles(self, mergefiles, watermark, pdf_steam):
     775        merger = PdfFileMerger()
     776        merger.append(StringIO(pdf_stream))
     777        for file in mergefiles:
     778            if watermark:
     779                # Pass through all pages of each file
     780                # and merge with watermark page. Paint
     781                # watermark first to make it transparent.
     782                marked_file = PdfFileWriter()
     783                orig_file = PdfFileReader(file)
     784                num_pages = orig_file.getNumPages()
     785                for num in range(num_pages):
     786                    watermark_file = PdfFileReader(watermark)
     787                    page = watermark_file.getPage(0)
     788                    page.mergePage(orig_file.getPage(num))
     789                    marked_file.addPage(page)
     790                # Save into a file-like object
     791                tmp1 = StringIO()
     792                marked_file.write(tmp1)
     793                # Append the file-like object
     794                merger.append(tmp1)
     795            else:
     796                # Just append the file object
     797                merger.append(file)
     798        # Save into a file-like object
     799        tmp2 = StringIO()
     800        merger.write(tmp2)
     801        return tmp2.getvalue()
     802
    774803    def renderPDF(self, view, filename='slip.pdf', student=None,
    775804                  studentview=None,
     
    777806                  note=None, signatures=None, sigs_in_footer=(),
    778807                  show_scans=True, topMargin=1.5,
    779                   omit_fields=()):
     808                  omit_fields=(),
     809                  mergefiles=None, watermark=None):
    780810        """Render pdf slips for various pages (also some pages
    781811        in the applicants module).
     
    867897                type="danger")
    868898            return view.redirect(view.url(view.context))
     899        if mergefiles:
     900            return self._mergeFiles(mergefiles, watermark, pdf_steam)
    869901        return pdf_stream
    870902
     
    940972            letterhead_path=letterhead_path)
    941973        if mergefiles:
    942             merger = PdfFileMerger()
    943             merger.append(StringIO(pdf_stream))
    944             for file in mergefiles:
    945                 if watermark:
    946                     # Pass through all pages of each file
    947                     # and merge with watermark page. Paint
    948                     # watermark first to make it transparent.
    949                     marked_file = PdfFileWriter()
    950                     orig_file = PdfFileReader(file)
    951                     num_pages = orig_file.getNumPages()
    952                     for num in range(num_pages):
    953                         watermark_file = PdfFileReader(watermark)
    954                         page = watermark_file.getPage(0)
    955                         page.mergePage(orig_file.getPage(num))
    956                         marked_file.addPage(page)
    957                     # Save into a file-like object
    958                     tmp1 = StringIO()
    959                     marked_file.write(tmp1)
    960                     # Append the file-like object
    961                     merger.append(tmp1)
    962                 else:
    963                     # Just append the file object
    964                     merger.append(file)
    965             # Save into a file-like object
    966             tmp2 = StringIO()
    967             merger.write(tmp2)
    968             return tmp2.getvalue()
     974            return self._mergeFiles(mergefiles, watermark, pdf_steam)
    969975        return pdf_stream
    970976
Note: See TracChangeset for help on using the changeset viewer.