GiroCode auf den Rechnungen
Bernd Wurst

Bernd Wurst commited on 2023-11-15 15:12:55
Zeige 3 geänderte Dateien mit 26 Einfügungen und 2 Löschungen.

... ...
@@ -3,6 +3,14 @@
3 3
 import datetime
4 4
 
5 5
 
6
+class Image:
7
+    def __init__(self, pilimage, caption=None, dpi=80, alignment="left"):
8
+        self.imagedata = pilimage
9
+        self.alignment = alignment
10
+        self.dpi = dpi
11
+        self.caption = caption
12
+
13
+
6 14
 class Text:
7 15
     def __init__(self, content, urgent=False, headline=None):
8 16
         self.paragraphs = [content]
... ...
@@ -10,7 +10,7 @@ from .metrics import *
10 10
 from .custom_elements import basicPage, firstPage, address_header
11 11
 
12 12
 # reportlab imports
13
-from reportlab.lib.units import cm
13
+from reportlab.lib.units import cm, inch
14 14
 from reportlab.pdfgen import canvas as Canvas
15 15
 from reportlab.lib.colors import Color
16 16
 
... ...
@@ -475,6 +475,19 @@ def InvoiceToPDF(iv, bankdata=True):
475 475
 
476 476
                 y -= line_padding * 3
477 477
             left, right = leftcontent, rightcontent
478
+        elif type(part) == Invoice.Image:
479
+            width = (part.imagedata.width / part.dpi) * inch
480
+            height = width * (part.imagedata.height / part.imagedata.width)
481
+            x = leftcontent
482
+            if part.alignment == "center":
483
+                x = leftcontent + (rightcontent - leftcontent)/2 - width/2
484
+            elif part.alignment == "right":
485
+                x = rightcontent - width
486
+            canvas.drawInlineImage(part.imagedata, x, y-height, width=width, height=height)
487
+            y -= line_padding + height
488
+            if part.caption:
489
+                canvas.drawString(x, y-font_height, part.caption)
490
+                y -= line_height
478 491
         else:
479 492
             raise NotImplementedError("Cannot handle part of type %s" % type(part))
480 493
         y -= (0.5 * cm)
... ...
@@ -1,7 +1,7 @@
1 1
 # -* coding: utf8 *-
2 2
 
3 3
 from __future__ import division
4
-from Invoice import Text, Table
4
+from Invoice import Text, Table, Image
5 5
 from utils import format_price, split_to_width
6 6
 
7 7
 
... ...
@@ -35,6 +35,9 @@ def InvoiceToText(iv, bankdata=True):
35 35
             ret.append(InvoiceTableToText(part))
36 36
         elif type(part) == Text:
37 37
             ret.append(InvoiceTextToText(part))
38
+        elif type(part) == Image:
39
+            # ignore images
40
+            pass
38 41
         else:
39 42
             raise NotImplementedError("Cannot handle part of type %s" % type(part))
40 43
 
41 44