コード例 #1
0
ファイル: server.py プロジェクト: 12foo/event-flyer-factory
def generate_preview(template, layout):
    if template not in templates.templates():
        return "Specified template does not exist.", 400
    if layout is None:
        layout = templates.templates()[template].layouts[0].__name__
    if layout not in layouts.layouts():
        return "Specified layout does not exist.", 400
    return send_file(pdf_builder.get_preview(template, layout), mimetype="image/jpeg")
コード例 #2
0
ファイル: server.py プロジェクト: yudevan/event-flyer-factory
def generate_preview(template, layout):
    if template not in templates.templates():
        return "Specified template does not exist.", 400
    if layout is None:
        layout = templates.templates()[template].layouts[0].__name__
    if layout not in layouts.layouts():
        return "Specified layout does not exist.", 400
    return send_file(pdf_builder.get_preview(template, layout),
                     mimetype="image/jpeg")
コード例 #3
0
ファイル: server.py プロジェクト: 12foo/event-flyer-factory
def build_flyer():
    if "layout" not in request.json or "template" not in request.json or "events" not in request.json:
        return "You have to specify template, a layout and a list of events.", 400
    template = request.json["template"]
    if template not in templates.templates():
        return "Specified template does not exist.", 400
    layout = request.json["layout"]
    if layout not in layouts.layouts():
        return "Specified layout does not exist.", 400
    with tempfile.NamedTemporaryFile(dir=tmpdir, delete=False) as event_pdf:
        pdf_builder.build_pdf(template, layout, request.json["events"], event_pdf.name)
        return json.jsonify(download=path.basename(event_pdf.name))
コード例 #4
0
ファイル: server.py プロジェクト: yudevan/event-flyer-factory
def build_flyer():
    if "layout" not in request.json or "template" not in request.json or "events" not in request.json:
        return "You have to specify template, a layout and a list of events.", 400
    template = request.json["template"]
    if template not in templates.templates():
        return "Specified template does not exist.", 400
    layout = request.json["layout"]
    if layout not in layouts.layouts():
        return "Specified layout does not exist.", 400
    with tempfile.NamedTemporaryFile(dir=tmpdir, delete=False) as event_pdf:
        pdf_builder.build_pdf(template, layout, request.json["events"],
                              event_pdf.name)
        return json.jsonify(download=path.basename(event_pdf.name))
コード例 #5
0
def build_pdf(template_name, layout_name, events, fname):
    with open(join("flyer-templates", template_name + ".pdf"), "rb") as template_file:
        template = PyPDF2.PdfFileReader(template_file)
        template_page = template.getPage(0)
        box = template_page.mediaBox
        pagesize = (float(box[2] - box[0]), float(box[3] - box[1]))
        layout = layouts.layouts()[layout_name]()
        with tempfile.NamedTemporaryFile() as event_pdf:
            layout.fill(event_pdf, pagesize, events, pagesize[1]/3, 0.5*inch, 0.5*inch)
            event_page = PyPDF2.PdfFileReader(event_pdf).getPage(0)
            template_page.mergePage(event_page)
            with open(fname, "wb") as out:
                merged = PyPDF2.PdfFileWriter()
                merged.addPage(template_page)
                merged.write(out)
コード例 #6
0
def build_pdf(template_name, layout_name, events, fname):
    with open(join("flyer-templates", template_name + ".pdf"),
              "rb") as template_file:
        template = PyPDF2.PdfFileReader(template_file)
        template_page = template.getPage(0)
        box = template_page.mediaBox
        pagesize = (float(box[2] - box[0]), float(box[3] - box[1]))
        layout = layouts.layouts()[layout_name]()
        with tempfile.NamedTemporaryFile() as event_pdf:
            layout.fill(event_pdf, pagesize, events, pagesize[1] / 3,
                        0.5 * inch, 0.5 * inch)
            event_page = PyPDF2.PdfFileReader(event_pdf).getPage(0)
            template_page.mergePage(event_page)
            with open(fname, "wb") as out:
                merged = PyPDF2.PdfFileWriter()
                merged.addPage(template_page)
                merged.write(out)