diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f08278d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.pdf \ No newline at end of file diff --git a/app.py b/app.py index 25a7b39..b2a9fd4 100644 --- a/app.py +++ b/app.py @@ -1,9 +1,23 @@ -from flask import Flask, render_template +from reportlab.lib.pagesizes import A4 +from reportlab.lib import colors +from reportlab.pdfgen import canvas +from reportlab.lib.units import mm from datetime import datetime import os import re +import textwrap -app = Flask(__name__) +# Konstanten für die Abmessungen +CIRCLE_RADIUS = 3 * mm +CIRCLE_DISTANCE = 80 * mm +TOP_MARGIN_TO_CENTER = 12 * mm +IMAGE_TOP_MARGIN = 28 * mm +IMAGE_HEIGHT = 60 * mm +TEXT_MARGIN = 5 * mm +LEFT_MARGIN_TO_CENTER = (A4[0] / 4) - (CIRCLE_DISTANCE / 2) # Zentriert zwischen den Tagen +IMAGE_SHIFT_UP = 1 * mm # Bild nach oben verschieben +DATE_SHIFT_DOWN = 8 * mm # Datum nach unten verschieben +TEXT_SHIFT_DOWN = 8 * mm # Text nach unten verschieben def get_dates_from_images(image_directory): # Regular expression to match files with a date format YYYY-MM-DD @@ -50,18 +64,81 @@ def get_image_and_text_for_date(date, image_directory, text_directory): return image_file, text_content +def draw_circle(c, x, y): + c.circle(x, y, CIRCLE_RADIUS, stroke=1, fill=0) -@app.route('/') -def calendar(): +def create_pdf(pages_data, output_filename): + c = canvas.Canvas(output_filename, pagesize=A4) + c.setCreator('Lorenz B.') + c.setTitle('Calendar') + c.setAuthor('Lorenz B.') + c.setSubject('calendar generator') + width, height = A4 + + max_text_width = (width / 2) - (2 * TEXT_MARGIN) + DATE_FONT_SIZE = 20 + DATE_FONT = "Helvetica-Bold" + + for page in pages_data: + for index, day in enumerate(page): + c.setDash(1, 0) + c.setStrokeColor(colors.black) + + # Berechnung der Positionen für jeden Tag + x = (index % 2) * (width / 2) + y = height - (index // 2 + 1) * (height / 2) + + # Höhere Position für das Bild + image_y_position = y + (height / 2 - IMAGE_TOP_MARGIN - IMAGE_HEIGHT) + IMAGE_SHIFT_UP + + # Kreise zeichnen + draw_circle(c, x + LEFT_MARGIN_TO_CENTER, y + height / 2 - TOP_MARGIN_TO_CENTER) + draw_circle(c, x + width / 2 - LEFT_MARGIN_TO_CENTER, y + height / 2 - TOP_MARGIN_TO_CENTER) + + # Bild einfügen + if day['image_file']: + c.drawImage(day['image_file'], x, image_y_position, width=width / 2, height=IMAGE_HEIGHT, preserveAspectRatio=True, anchor='n') + + # Datum direkt unter dem Bild einfügen + date_x_position = x + (width / 4) # Zentrum des Tagesbereichs + date_y_position = image_y_position - DATE_SHIFT_DOWN + date_str = day['date'].strftime('%A, %d.%m.%Y') + c.setFont(DATE_FONT, DATE_FONT_SIZE) # Schriftart und Schriftgröße setzen + date_width = c.stringWidth(date_str, DATE_FONT, DATE_FONT_SIZE) + c.drawString(date_x_position - (date_width / 2), date_y_position, date_str) + + # Text unter dem Datum einfügen + text_y_position = date_y_position - TEXT_SHIFT_DOWN - 5 * mm # Extra Abstand nach einem größeren Datum + if day['text_content']: + c.setFont("Helvetica", 12) # Schriftart zurücksetzen für den Text + wrapped_text = textwrap.fill(day['text_content'], width=50) # Anpassen für die passende Zeilenlänge + text = c.beginText(x + TEXT_MARGIN, text_y_position) + for line in wrapped_text.split('\n'): + text.textLine(line) + c.drawText(text) + + # Gestrichelte Linien zeichnen, falls notwendig + c.setDash(1, 2) + c.setStrokeColor(colors.grey) + if index % 2 == 0: # Vertikale Linie rechts für den Tag + c.line(x + width / 2, y, x + width / 2, y + height / 2) + if index < 2: # Horizontale Linie unten für den Tag + c.line(x, y, x + width / 2, y) + + c.showPage() + c.save() + +def main(): BASE_DIR = os.path.dirname(os.path.abspath(__file__)) - image_directory = os.path.join(BASE_DIR, 'static', 'images') + image_directory = os.path.join(BASE_DIR, 'images') text_directory = os.path.join(BASE_DIR, 'texts') # Get sorted dates from image files dates = get_dates_from_images(image_directory) if not dates: - return "No images found with the correct date format in the name.", 404 + print("No images found with the correct date format in the name.") + return # Generate data for each date pages_data = [] @@ -83,7 +160,11 @@ def calendar(): if days_data: pages_data.append(days_data) - return render_template('index.html', pages=pages_data) + # Create the PDF + output_filename = os.path.join(BASE_DIR, 'calendar.pdf') + create_pdf(pages_data, output_filename) + + print("PDF created successfully.") if __name__ == '__main__': - app.run(debug=True) + main() diff --git a/images/.gitignore b/images/.gitignore new file mode 100644 index 0000000..0f75fc0 --- /dev/null +++ b/images/.gitignore @@ -0,0 +1,3 @@ +*.png +*.jpeg +*.jpg \ No newline at end of file diff --git a/static/images/2024-01-15.jpg b/static/images/2024-01-15.jpg deleted file mode 100755 index b0e361c..0000000 Binary files a/static/images/2024-01-15.jpg and /dev/null differ diff --git a/templates/index.html b/templates/index.html deleted file mode 100644 index 3bc9e61..0000000 --- a/templates/index.html +++ /dev/null @@ -1,107 +0,0 @@ - - - - - - - - - {% for page in pages %} -
- {% for day in page %} -
-
-
-
- {% if day.image_file %} - Bild für {{ day.date.strftime('%d. %B %Y') }} - {% endif %} -
-
-

{{ day.date.strftime('%A %d.%m.%Y') }}

-
-
- {% if day.text_content %} -

{{ day.text_content }}

- {% endif %} -
-
- {% endfor %} -
- {% endfor %} - - - - diff --git a/texts/.gitignore b/texts/.gitignore new file mode 100644 index 0000000..314f02b --- /dev/null +++ b/texts/.gitignore @@ -0,0 +1 @@ +*.txt \ No newline at end of file diff --git a/texts/2024-01-15.txt b/texts/2024-01-15.txt deleted file mode 100755 index 6266b15..0000000 --- a/texts/2024-01-15.txt +++ /dev/null @@ -1 +0,0 @@ -Beispiel Text \ No newline at end of file