Mercurial > odfcast
changeset 19:2f627039d2b4
Add a MergeView to merge pdfs based on PyPDF2
author | Björn Ricks <bjoern.ricks@intevation.de> |
---|---|
date | Mon, 29 Sep 2014 15:59:36 +0200 |
parents | 585904e7411a |
children | c289263e1b24 |
files | odfcast/convert.py templates/merge.html |
diffstat | 2 files changed, 39 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/odfcast/convert.py Mon Sep 29 14:18:11 2014 +0200 +++ b/odfcast/convert.py Mon Sep 29 15:59:36 2014 +0200 @@ -7,14 +7,19 @@ from py3o.template import Template +from PyPDF2 import PdfFileMerger + + ALLOWED_FORMATS = ["pdf", "doc", "docx", "odt"] +PDF_MIMETYPE = "application/pdf" + MIMETYPES = { "odt": "application/vnd.oasis.opendocument.text", "doc": "application/msword", "docx": "application/vnd.openxmlformats-officedocument" ".wordprocessingml.document", - "pdf": "application/pdf", + "pdf": PDF_MIMETYPE, } DEFAULT_MIMETYPE = "application/octet-stream" @@ -92,3 +97,23 @@ def get(self): return render_template("template_convert.html") + + +class MergeView(MethodView): + + def get(self): + return render_template("merge.html") + + def post(self): + merger = PdfFileMerger() + ffiles = request.files.getlist('files') + + for ffile in ffiles: + merger.append(ffile) + + outfile = tempfile.NamedTemporaryFile() + + merger.write(outfile.name) + + merger.close() + return Response(outfile, mimetype=PDF_MIMETYPE)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/templates/merge.html Mon Sep 29 15:59:36 2014 +0200 @@ -0,0 +1,13 @@ +{% extends 'base.html' %} + +{% block content %} +<h1>Merge</h1> + +<form role="form" action="" method="POST" enctype="multipart/form-data"> + <div class="form-group"> + <label for="files">Files</label> + <input id="files" type="file" name="files" multiple> + </div> + <button class="btn btn-default" type="submit">Convert</button> +</form> +{% endblock %}