diff --git a/cli.py b/cli.py index 85b1815..fe599b2 100755 --- a/cli.py +++ b/cli.py @@ -1,13 +1,13 @@ #! /usr/bin/env python3 from sys import argv -from convert import convert_dhl, convert_hermes +from convert import * if __name__ == "__main__": if len(argv) != 3: print("Usage:") print("./cli.py ") - print("Method can be 'hermes' or 'dhl'") + print("Method can be 'hermes', 'dhl' or 'vinted'") exit(1) method = argv[1] @@ -18,6 +18,8 @@ if __name__ == "__main__": convert_dhl(id) case "hermes": convert_hermes(id) + case "vinted": + convert_vinted(id) case _: print("Unknown method") diff --git a/convert.py b/convert.py index 46a85e5..b4a78b9 100644 --- a/convert.py +++ b/convert.py @@ -50,3 +50,27 @@ def convert_hermes(id): with open(f"./downloads/{id}.pdf", "wb") as fh: writer.write(fh) + +def convert_vinted(id): + with open(f"./uploads/{id}.pdf", "rb") as fh: + reader = PdfReader(fh) + writer = PdfWriter() + + numPages = len(reader.pages) + if numPages != 1: + exit(1) + + #page = reader.pages[0].rotate(90) + page = reader.pages[0] + + left = 18 + bottom = 150 + right = 315 + # calculate last value to get a 3:2 rectangle + top = bottom+((right-left)/2)*3 + + page.mediabox = RectangleObject((left, bottom,right,top)) + writer.add_page(page) + + with open(f"./downloads/{id}.pdf", "wb") as fh: + writer.write(fh) diff --git a/main.py b/main.py index 5c332dc..06b8950 100644 --- a/main.py +++ b/main.py @@ -3,7 +3,7 @@ from bottle import route, request, static_file, run, redirect, HTTPResponse, hoo from random import randint from os import remove -from convert import convert_dhl, convert_hermes +from convert import * @route('/') def root(): @@ -39,6 +39,21 @@ def hermes(): return redirect(f"/download/{id}") +@route('/vinted', method='POST') +def vinted(): + upload = request.files.get('upload') + if upload.content_type != "application/pdf": + return HTTPResponse(status=400, body="Bad file") + + id = randint(100000,999999) + upload.save(f"./uploads/{id}.pdf") + convert_vinted(id) + print(f"converted {upload.filename} as {id}.pdf") + # delete upload file + remove(f"./uploads/{id}.pdf") + + return redirect(f"/download/{id}") + @route('/download/') def download(id): filename = f"{id}.pdf" diff --git a/templates/index.html b/templates/index.html index 88561d3..ce984ec 100644 --- a/templates/index.html +++ b/templates/index.html @@ -22,6 +22,7 @@ +
Variante 2
Hermes Paketschein DIN A4 → 100mmx150mm @@ -30,6 +31,15 @@
+ +
+ Variante 3
+ Vinted Paketschein DIN A4 → 100mmx150mm +
+ + +
+