Advertisement
MDSJK

Rechnung

Dec 14th, 2023 (edited)
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.85 KB | Source Code | 0 0
  1. import sys
  2. import json
  3. from reportlab.lib.pagesizes import letter
  4. from reportlab.pdfgen import canvas
  5. from reportlab.lib import colors
  6.  
  7. def create_invoice(invoice_data, filename):
  8.     c = canvas.Canvas(filename, pagesize=letter)
  9.     c.setLineWidth(.3)
  10.     c.setFont('Helvetica', 12)
  11.     width, height = letter  # Canvas size for layout calculations
  12.  
  13.     # Set colors for design
  14.     black = colors.black
  15.     green = colors.green
  16.  
  17.     # Draw the Logo
  18.     c.drawInlineImage('C:\\Projekte\\rechnung\\MSLogo.png', 30, height - 80, width=100, height=50)
  19.  
  20.  
  21.  
  22.     # Set provider and customer data
  23.     provider_x = 30
  24.     customer_x = 400
  25.     info_y = height - 130
  26.     c.setFillColor(black)
  27.     c.drawString(provider_x, info_y, invoice_data["provider_name"])
  28.     c.drawString(provider_x, info_y - 15, invoice_data["provider_address"])
  29.     c.drawString(provider_x, info_y - 30, 'Mobil: ' + invoice_data["provider_mobile"])
  30.     c.drawString(provider_x, info_y - 45, 'E-Mail: ' + invoice_data["provider_email"])
  31.    
  32.     # Adjusted customer data layout
  33.     c.drawString(customer_x, info_y, invoice_data["customer_name"])
  34.     c.drawString(customer_x, info_y - 15, invoice_data["customer_address_line1"])
  35.     c.drawString(customer_x, info_y - 30, invoice_data["customer_address_line2"])
  36.     c.drawString(customer_x, info_y - 45, 'Mobil: ' + invoice_data["customer_mobile"])
  37.  
  38.     # Rechnungsnummer, Datum und Kundennummer
  39.     info_y = height - 200
  40.     c.drawString(provider_x, info_y, 'Rechnungsnummer: ' + invoice_data["invoice_number"])
  41.     c.drawString(provider_x, info_y - 15, 'Datum: ' + invoice_data["date"])
  42.     c.drawString(provider_x, info_y - 30, 'Kundennummer: ' + invoice_data["customer_number"])
  43.  
  44.     # Leistungsdetails
  45.     service_y = info_y - 80
  46.     c.drawString(provider_x, service_y, 'Bezeichnung')
  47.     c.drawString(provider_x + 200, service_y, 'Dauer')
  48.     c.drawString(provider_x + 300, service_y, 'Beschreibung')  
  49.     c.drawString(475, service_y, 'Preis')
  50.     c.drawString(530, service_y, 'Total')
  51.  
  52.     # Draw line
  53.     c.setStrokeColor(green)
  54.     c.line(30, service_y - 5, 580, service_y - 5)
  55.  
  56.     for item in invoice_data["items"]:
  57.         service_y -= 20
  58.         c.setFillColor(black)
  59.         c.drawString(provider_x, service_y, item["title"])
  60.         c.drawString(provider_x + 200, service_y, item["description"])
  61.         c.drawString(provider_x + 300, service_y, 'siehe Inhalt 1. Gespräch')  
  62.         c.drawString(475, service_y, "{:.2f} €".format(item["price"]))
  63.         c.drawString(530, service_y, "{:.2f} €".format(item["total"]))
  64.    
  65.     # Draw line after items
  66.     c.setStrokeColor(green)
  67.     c.line(30, service_y - 5, 580, service_y - 5)
  68.  
  69.      # Total
  70.     c.setFillColor(black)
  71.     total_label = 'Gesamt Brutto:'
  72.     total_value = "{:.2f} €".format(invoice_data["total"])
  73.     total_label_x = 430
  74.     total_value_x = 530
  75.     total_y = service_y - 20
  76.  
  77.     c.drawString(total_label_x, total_y, total_label)
  78.     c.drawString(total_value_x, total_y, total_value)
  79.  
  80.     # Draw underline for total
  81.     underline_start = total_label_x
  82.     underline_end = total_value_x + c.stringWidth(total_value, 'Helvetica', 12)
  83.     underline_y = total_y - 3
  84.     c.setStrokeColor(black)
  85.     c.line(underline_start, underline_y, underline_end, underline_y)
  86.  
  87.     # Activity description
  88.     c.setFont('Helvetica', 10)
  89.     tätigkeitsbeschreibung_y = service_y - 40
  90.     c.drawString(provider_x, tätigkeitsbeschreibung_y, "Inhalte 1. Gespräch:")
  91.     beschreibungen = [
  92.         "- Gemeinsame Zielsetzung inkl.realistischer Bewertung",
  93.         "- Individuelle Betrachtung des Ist Zustandes.",
  94.         "- Beantworten von Fragen zur Beratung bzw. des Projektes (Gesamtdauer, Abrechnung usw.)",
  95.         "- Bewusstsein schaffen, Eigenverantwortung vermitteln!",
  96.         "  Was ist nötig um das gewählte Ziel zu erreichen?",
  97.         "- Ausstieg möglich! Nur 100 Euro investiert! (Schutz vor möglichen Fehlentscheidungen, Fehlkäufen usw.)"
  98.     ]
  99.  
  100.     for beschreibung in beschreibungen:
  101.         tätigkeitsbeschreibung_y -= 15
  102.         c.drawString(provider_x, tätigkeitsbeschreibung_y, beschreibung)
  103.  
  104.     # Additional Info
  105.     additional_info_y = tätigkeitsbeschreibung_y - 30
  106.     c.drawString(provider_x, additional_info_y, "Gemäß § 19 UStG wird keine Umsatzsteuer berechnet!")
  107.     c.drawString(provider_x, additional_info_y - 15, "Zahlung innerhalb von 14 Tagen ohne Abzug!")
  108.     c.drawString(provider_x, additional_info_y - 30, "Bitte geben Sie als Verwendungszweck die Rechnungsnummer und Ihren Namen/Kundennummer an!")
  109.  
  110.     # Footer info
  111.     c.setFont('Helvetica', 9)
  112.     bank_info_start_y = contact_info_start_y = 80  
  113.  
  114.     # Bank info
  115.     bank_info_lines = [
  116.         'Steuernummer: 020/263/02334',
  117.         'Bank: Vereinigte Volksbank eG',
  118.         'IBAN: DE71 5909 2000 5548 8802 05',
  119.         'BIC: GENODE51SB2'
  120.     ]
  121.     right_margin = 30
  122.     bank_info_x = width - right_margin
  123.     for line in bank_info_lines:
  124.         text_width = c.stringWidth(line, 'Helvetica', 9)
  125.         c.drawString(bank_info_x - text_width, bank_info_start_y, line)
  126.         bank_info_start_y -= 15
  127.  
  128.     # Kontaktinformationen
  129.     contact_info_lines = [
  130.         'Ernährungsberatung - beispiel',
  131.         'Mühlenteich 30',
  132.         '66674 beispielshausen',
  133.         'Mobil: 0170-2544545',
  134.         'Email: beispiel@gmail.com'
  135.     ]
  136.     left_margin = 30
  137.     contact_info_x = left_margin
  138.     for line in contact_info_lines:
  139.         c.drawString(contact_info_x, contact_info_start_y, line)
  140.         contact_info_start_y -= 15
  141.  
  142.     # Spruch
  143.     spruch = "Ihr Weg - unsere Unterstützung!"
  144.     spruch_font_size = 14
  145.     c.setFont('Helvetica', spruch_font_size)
  146.     text_width = c.stringWidth(spruch, 'Helvetica', spruch_font_size)
  147.     c.drawString((width - text_width) / 2.0, 230, spruch)
  148.  
  149.     # New section between slogan and footer
  150.     slogan_y = 160  
  151.     c.setFont('Helvetica', 12)
  152.     phrases = ["ENTSCHEIDUNG", "BERATUNG", "VERÄNDERUNG", "ERFOLG"]
  153.    
  154.     # Set starting and ending x positions
  155.     start_x = left_margin
  156.     end_x = width - right_margin - c.stringWidth(phrases[-1], 'Helvetica', 12)
  157.  
  158.     # Calculate the total width of the middle words
  159.     middle_words_width = sum(c.stringWidth(word, 'Helvetica', 12) for word in phrases[1:-1])
  160.     # Calculate the space available excluding the width of the middle words
  161.     available_space = end_x - start_x - c.stringWidth(phrases[0], 'Helvetica', 12)
  162.     # Calculate the gaps between the words
  163.     gap = (available_space - middle_words_width) / 3
  164.  
  165.     # Draw the first word at the start
  166.     c.setFillColor(black)
  167.     c.drawString(start_x, slogan_y, phrases[0])
  168.  
  169.     # Draw the middle words with equal gaps
  170.     current_x = start_x + c.stringWidth(phrases[0], 'Helvetica', 12) + gap
  171.     for word in phrases[1:-1]:
  172.         c.setFillColor(green if word == "BERATUNG" else black)
  173.         c.drawString(current_x, slogan_y, word)
  174.         current_x += c.stringWidth(word, 'Helvetica', 12) + gap
  175.  
  176.     # Draw the last word at the end
  177.     c.setFillColor(green)
  178.     c.drawString(end_x, slogan_y, phrases[-1])
  179.  
  180.     c.save()
  181.  
  182. # Daten für die Rechnung
  183. invoice_data = {
  184.     "provider_name": "Nico Rosar",
  185.     "provider_address": "Mühlenteich 30, 66674 beispiel",
  186.     "provider_mobile": "0170 | 29 25 451",
  187.     "provider_email": "beispiel@gmail.com",
  188.     "customer_name": "Julian Klasen",
  189.     "customer_address_line1": "Bachstraße 103",
  190.     "customer_address_line2": "66674 beispiel",
  191.     "customer_mobile": "017162581251",
  192.     "date": "06.09.2023",
  193.     "invoice_number": "001",
  194.     "customer_number": "K003",
  195.     "items": [
  196.         {
  197.             "title": "Ernährungsberatung Gespräch 1",
  198.             "description": "ca. 2 Stunden",
  199.             "price": 100.00,
  200.             "total": 100.00
  201.         }
  202.     ],
  203.     "total": 100.00
  204. }
  205.  
  206. # Rechnung erstellen
  207. create_invoice(invoice_data, 'Rechnung_Gespraech1.pdf')
  208.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement