Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import json
- from reportlab.lib.pagesizes import letter
- from reportlab.pdfgen import canvas
- from reportlab.lib import colors
- def create_invoice(invoice_data, filename):
- c = canvas.Canvas(filename, pagesize=letter)
- c.setLineWidth(.3)
- c.setFont('Helvetica', 12)
- width, height = letter # Canvas size for layout calculations
- # Set colors for design
- black = colors.black
- green = colors.green
- # Draw the Logo
- c.drawInlineImage('C:\\Projekte\\rechnung\\MSLogo.png', 30, height - 80, width=100, height=50)
- # Set provider and customer data
- provider_x = 30
- customer_x = 400
- info_y = height - 130
- c.setFillColor(black)
- c.drawString(provider_x, info_y, invoice_data["provider_name"])
- c.drawString(provider_x, info_y - 15, invoice_data["provider_address"])
- c.drawString(provider_x, info_y - 30, 'Mobil: ' + invoice_data["provider_mobile"])
- c.drawString(provider_x, info_y - 45, 'E-Mail: ' + invoice_data["provider_email"])
- # Adjusted customer data layout
- c.drawString(customer_x, info_y, invoice_data["customer_name"])
- c.drawString(customer_x, info_y - 15, invoice_data["customer_address_line1"])
- c.drawString(customer_x, info_y - 30, invoice_data["customer_address_line2"])
- c.drawString(customer_x, info_y - 45, 'Mobil: ' + invoice_data["customer_mobile"])
- # Rechnungsnummer, Datum und Kundennummer
- info_y = height - 200
- c.drawString(provider_x, info_y, 'Rechnungsnummer: ' + invoice_data["invoice_number"])
- c.drawString(provider_x, info_y - 15, 'Datum: ' + invoice_data["date"])
- c.drawString(provider_x, info_y - 30, 'Kundennummer: ' + invoice_data["customer_number"])
- # Leistungsdetails
- service_y = info_y - 80
- c.drawString(provider_x, service_y, 'Bezeichnung')
- c.drawString(provider_x + 200, service_y, 'Dauer')
- c.drawString(provider_x + 300, service_y, 'Beschreibung')
- c.drawString(475, service_y, 'Preis')
- c.drawString(530, service_y, 'Total')
- # Draw line
- c.setStrokeColor(green)
- c.line(30, service_y - 5, 580, service_y - 5)
- for item in invoice_data["items"]:
- service_y -= 20
- c.setFillColor(black)
- c.drawString(provider_x, service_y, item["title"])
- c.drawString(provider_x + 200, service_y, item["description"])
- c.drawString(provider_x + 300, service_y, 'siehe Inhalt 1. Gespräch')
- c.drawString(475, service_y, "{:.2f} €".format(item["price"]))
- c.drawString(530, service_y, "{:.2f} €".format(item["total"]))
- # Draw line after items
- c.setStrokeColor(green)
- c.line(30, service_y - 5, 580, service_y - 5)
- # Total
- c.setFillColor(black)
- total_label = 'Gesamt Brutto:'
- total_value = "{:.2f} €".format(invoice_data["total"])
- total_label_x = 430
- total_value_x = 530
- total_y = service_y - 20
- c.drawString(total_label_x, total_y, total_label)
- c.drawString(total_value_x, total_y, total_value)
- # Draw underline for total
- underline_start = total_label_x
- underline_end = total_value_x + c.stringWidth(total_value, 'Helvetica', 12)
- underline_y = total_y - 3
- c.setStrokeColor(black)
- c.line(underline_start, underline_y, underline_end, underline_y)
- # Activity description
- c.setFont('Helvetica', 10)
- tätigkeitsbeschreibung_y = service_y - 40
- c.drawString(provider_x, tätigkeitsbeschreibung_y, "Inhalte 1. Gespräch:")
- beschreibungen = [
- "- Gemeinsame Zielsetzung inkl.realistischer Bewertung",
- "- Individuelle Betrachtung des Ist Zustandes.",
- "- Beantworten von Fragen zur Beratung bzw. des Projektes (Gesamtdauer, Abrechnung usw.)",
- "- Bewusstsein schaffen, Eigenverantwortung vermitteln!",
- " Was ist nötig um das gewählte Ziel zu erreichen?",
- "- Ausstieg möglich! Nur 100 Euro investiert! (Schutz vor möglichen Fehlentscheidungen, Fehlkäufen usw.)"
- ]
- for beschreibung in beschreibungen:
- tätigkeitsbeschreibung_y -= 15
- c.drawString(provider_x, tätigkeitsbeschreibung_y, beschreibung)
- # Additional Info
- additional_info_y = tätigkeitsbeschreibung_y - 30
- c.drawString(provider_x, additional_info_y, "Gemäß § 19 UStG wird keine Umsatzsteuer berechnet!")
- c.drawString(provider_x, additional_info_y - 15, "Zahlung innerhalb von 14 Tagen ohne Abzug!")
- c.drawString(provider_x, additional_info_y - 30, "Bitte geben Sie als Verwendungszweck die Rechnungsnummer und Ihren Namen/Kundennummer an!")
- # Footer info
- c.setFont('Helvetica', 9)
- bank_info_start_y = contact_info_start_y = 80
- # Bank info
- bank_info_lines = [
- 'Steuernummer: 020/263/02334',
- 'Bank: Vereinigte Volksbank eG',
- 'IBAN: DE71 5909 2000 5548 8802 05',
- 'BIC: GENODE51SB2'
- ]
- right_margin = 30
- bank_info_x = width - right_margin
- for line in bank_info_lines:
- text_width = c.stringWidth(line, 'Helvetica', 9)
- c.drawString(bank_info_x - text_width, bank_info_start_y, line)
- bank_info_start_y -= 15
- # Kontaktinformationen
- contact_info_lines = [
- 'Ernährungsberatung - beispiel',
- 'Mühlenteich 30',
- '66674 beispielshausen',
- 'Mobil: 0170-2544545',
- 'Email: beispiel@gmail.com'
- ]
- left_margin = 30
- contact_info_x = left_margin
- for line in contact_info_lines:
- c.drawString(contact_info_x, contact_info_start_y, line)
- contact_info_start_y -= 15
- # Spruch
- spruch = "Ihr Weg - unsere Unterstützung!"
- spruch_font_size = 14
- c.setFont('Helvetica', spruch_font_size)
- text_width = c.stringWidth(spruch, 'Helvetica', spruch_font_size)
- c.drawString((width - text_width) / 2.0, 230, spruch)
- # New section between slogan and footer
- slogan_y = 160
- c.setFont('Helvetica', 12)
- phrases = ["ENTSCHEIDUNG", "BERATUNG", "VERÄNDERUNG", "ERFOLG"]
- # Set starting and ending x positions
- start_x = left_margin
- end_x = width - right_margin - c.stringWidth(phrases[-1], 'Helvetica', 12)
- # Calculate the total width of the middle words
- middle_words_width = sum(c.stringWidth(word, 'Helvetica', 12) for word in phrases[1:-1])
- # Calculate the space available excluding the width of the middle words
- available_space = end_x - start_x - c.stringWidth(phrases[0], 'Helvetica', 12)
- # Calculate the gaps between the words
- gap = (available_space - middle_words_width) / 3
- # Draw the first word at the start
- c.setFillColor(black)
- c.drawString(start_x, slogan_y, phrases[0])
- # Draw the middle words with equal gaps
- current_x = start_x + c.stringWidth(phrases[0], 'Helvetica', 12) + gap
- for word in phrases[1:-1]:
- c.setFillColor(green if word == "BERATUNG" else black)
- c.drawString(current_x, slogan_y, word)
- current_x += c.stringWidth(word, 'Helvetica', 12) + gap
- # Draw the last word at the end
- c.setFillColor(green)
- c.drawString(end_x, slogan_y, phrases[-1])
- c.save()
- # Daten für die Rechnung
- invoice_data = {
- "provider_name": "Nico Rosar",
- "provider_address": "Mühlenteich 30, 66674 beispiel",
- "provider_mobile": "0170 | 29 25 451",
- "provider_email": "beispiel@gmail.com",
- "customer_name": "Julian Klasen",
- "customer_address_line1": "Bachstraße 103",
- "customer_address_line2": "66674 beispiel",
- "customer_mobile": "017162581251",
- "date": "06.09.2023",
- "invoice_number": "001",
- "customer_number": "K003",
- "items": [
- {
- "title": "Ernährungsberatung Gespräch 1",
- "description": "ca. 2 Stunden",
- "price": 100.00,
- "total": 100.00
- }
- ],
- "total": 100.00
- }
- # Rechnung erstellen
- create_invoice(invoice_data, 'Rechnung_Gespraech1.pdf')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement