From ce057bd4b473fea5fb1ae5644a01b020e91c838c Mon Sep 17 00:00:00 2001
From: fleaz <mail@felixbreidenstein.de>
Date: Sun, 20 Oct 2024 14:24:36 +0200
Subject: [PATCH] Added support for 'Hermes Paketschein'

---
 convert.py           | 24 ++++++++++++++++++++++++
 main.py              | 19 +++++++++++++++++--
 templates/index.html |  8 ++++++++
 3 files changed, 49 insertions(+), 2 deletions(-)

diff --git a/convert.py b/convert.py
index e801d5e..46a85e5 100644
--- a/convert.py
+++ b/convert.py
@@ -26,3 +26,27 @@ def convert_dhl(id):
 
         with open(f"./downloads/{id}.pdf", "wb") as fh:
             writer.write(fh)
+
+def convert_hermes(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 = 30
+        bottom = 452
+        right = 564
+        # calculate last value to get a 3:2 rectangle
+        top = bottom+((right-left)/3)*2
+
+        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 7fddb9c..5c332dc 100644
--- a/main.py
+++ b/main.py
@@ -3,14 +3,14 @@ from bottle import route, request, static_file, run, redirect, HTTPResponse, hoo
 from random import randint
 from os import remove
 
-from convert import convert_dhl
+from convert import convert_dhl, convert_hermes
 
 @route('/')
 def root():
     return static_file('index.html', root='templates')
 
 @route('/dhl', method='POST')
-def do_upload():
+def dhl():
     upload = request.files.get('upload')
     if upload.content_type != "application/pdf":
         return HTTPResponse(status=400, body="Bad file")
@@ -24,6 +24,21 @@ def do_upload():
 
     return redirect(f"/download/{id}")
 
+@route('/hermes', method='POST')
+def hermes():
+    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_hermes(id)
+    print(f"converted {upload.filename} as {id}.pdf")
+    # delete upload file
+    remove(f"./uploads/{id}.pdf")
+
+    return redirect(f"/download/{id}")
+
 @route('/download/<id>')
 def download(id):
     filename = f"{id}.pdf"
diff --git a/templates/index.html b/templates/index.html
index 9052286..88561d3 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -22,6 +22,14 @@
               <button>Convert!</button>
             </form>
         </div>
+        <div class="variant_box">
+            <b>Variante 2</b><br>
+            Hermes Paketschein DIN A4 &#8594; 100mmx150mm
+            <form action="/hermes" method="post" enctype="multipart/form-data">
+              <input name="upload" type="file" size="50" accept="application/pdf"> 
+              <button>Convert!</button>
+            </form>
+        </div>
     </main>
   </body>
 </html>