Esempio n. 1
0
    def generate_skript(skript):  # appending skript file to cover

        cover_path = SkriptGenerator.cover_dir + "Deckblatt_" + skript.article_number + ".pdf"
        skript_path = SkriptGenerator.skript_dir + "SkriptFile_" + skript.article_number + ".pdf"
        finish_path = SkriptGenerator.finish_dir + "Skript_" + skript.article_number + ".pdf"

        # remove old skript file
        FileSystemStorage().delete(finish_path)

        if not FileSystemStorage().exists(skript_path):  # no file available, abort
            return

        # (re-)generate cover
        SkriptGenerator.generate_cover(skript)

        try:
            merger = PdfFileMerger()
            merger.append(PdfFileReader(FileSystemStorage().open(cover_path, 'rb')))
            merger.append(PdfFileReader(FileSystemStorage().open(skript_path, 'rb')))
            output_stream = FileSystemStorage().open(finish_path, 'wb')
            merger.write(output_stream)
            output_stream.close()
        except:
            return -1  # error creating the skript

        return "Skript_" + skript.article_number + ".pdf"
Esempio n. 2
0
    def generate_cover_old(skript):
        # from http://allfont.de/download/comic-sans-ms-bold/ (not for commercial use)
        pdfmetrics.registerFont(TTFont('Comic', SkriptGenerator.font_dir + 'comic-sans-ms.ttf'))
        pdfmetrics.registerFont(TTFont('Comic bold', SkriptGenerator.font_dir + 'comic-sans-ms-bold.ttf'))
        packet = io.BytesIO()
        text1 = skript.name
        text2 = ""
        # splitting title appropriately
        pos = text1.find(':')
        if pos >= 20:  # split line after :
            text1 = skript.name[0:pos + 1]
            text2 = skript.name[pos + 1:].strip()
        elif len(text1) > 40:  # split line longer than 40 chars
            tokens = text1.split()
            text1 = ""
            text2 = ""
            for token in tokens:
                if len(text1) > 30:
                    text2 = text2 + " " + token
                else:
                    text1 = text1 + " " + token
        default_font = "Comic bold"
        id1 = {
            "text": skript.article_number,
            "font": default_font,
            "size": 28,
            "x-pos": 147 * mm,
            "y-pos": 262 * mm,
        }
        id2 = {
            "text": id1["text"],
            "font": default_font,
            "size": 28,
            "x-pos": 147 * mm,
            "y-pos": 34 * mm,
        }
        title = {
            "text": text1,
            "font": default_font,
            "size": 18,
            "x-pos": 34 * mm,
            "y-pos": 209 * mm,
        }
        title2 = {
            "text": text2,
            "font": title["font"],
            "size": title["size"],
            "x-pos": title["x-pos"],
            "y-pos": 201 * mm,
        }
        arrow = {
            "text": ">>",
            "font": title["font"],
            "size": title["size"],
            "x-pos": 23 * mm,
            "y-pos": title["y-pos"],
        }
        author = {
            "text": skript.author.last_name + ", " + skript.author.title + " " + skript.author.first_name,
            "font": "Comic",
            "size": 16,
            "x-pos": 34 * mm,
            "y-pos": 166 * mm,
        }
        insertions = [id1, id2, arrow, title, title2, author]
        can = canvas.Canvas(packet, pagesize=A4)
        for insert in insertions:
            can.setFont(insert["font"], insert["size"])
            can.drawString(insert["x-pos"], insert["y-pos"], insert["text"])
        can.save()
        # move to the beginning of the StringIO buffer
        packet.seek(0)
        new_pdf = PdfFileReader(packet)
        # read PDF template
        existing_pdf = PdfFileReader(FileSystemStorage().open(SkriptGenerator.template_path, 'rb'))

        output = PdfFileWriter()
        # add the "watermark" (which is the new pdf) on the existing page
        page = new_pdf.getPage(0)
        page.mergePage(existing_pdf.getPage(0))
        output.addPage(page)
        # appending the following pages of cover template
        for i in range(1, existing_pdf.getNumPages()):
            output.addPage(existing_pdf.getPage(i))
        # finally, write "output" to a real file

        outputStream = FileSystemStorage().open(SkriptGenerator.cover_dir + "Deckblatt_" + skript.article_number + ".pdf", "wb")
        output.write(outputStream)
        outputStream.close()
        return "Deckblatt_" + skript.article_number + ".pdf"