Update app.py
Browse files
app.py
CHANGED
@@ -10,7 +10,7 @@ from PIL import Image
|
|
10 |
import fitz # PyMuPDF
|
11 |
|
12 |
from reportlab.lib.pagesizes import A4
|
13 |
-
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle
|
14 |
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
15 |
from reportlab.lib import colors
|
16 |
from reportlab.pdfbase import pdfmetrics
|
@@ -160,7 +160,7 @@ default_markdown = """# π Deities Guide: Mythology and Moral Lessons π
|
|
160 |
14. **Hindu: Avatars & Devis** πΊ
|
161 |
1. Krishna, Rama, Durga: Sons and fierce mother.
|
162 |
2. Relation: Avatars and goddess protect dharma.
|
163 |
-
|
164 |
15. **Japanese** πΈ
|
165 |
1. Amaterasu, Susanoo, Tsukuyomi: Sister, brothers.
|
166 |
2. Relation: Siblings balance cosmos.
|
@@ -216,7 +216,7 @@ default_markdown = """# π Deities Guide: Mythology and Moral Lessons π
|
|
216 |
"""
|
217 |
|
218 |
# ---------------------------------------------------------------
|
219 |
-
# Process markdown into a two-page layout
|
220 |
def markdown_to_pdf_content(markdown_text):
|
221 |
lines = markdown_text.strip().split('\n')
|
222 |
pdf_content = []
|
@@ -261,18 +261,18 @@ def markdown_to_pdf_content(markdown_text):
|
|
261 |
if current_item and sub_items:
|
262 |
pdf_content.append([current_item, sub_items])
|
263 |
|
264 |
-
# Split
|
265 |
-
|
266 |
-
|
267 |
page1_content = []
|
268 |
page2_content = []
|
269 |
-
|
270 |
|
271 |
for item in pdf_content:
|
272 |
-
|
273 |
-
if
|
274 |
page1_content.append(item)
|
275 |
-
|
276 |
else:
|
277 |
page2_content.append(item)
|
278 |
|
@@ -360,7 +360,7 @@ def create_main_pdf(markdown_text, base_font_size=10, auto_size=False):
|
|
360 |
else:
|
361 |
story.append(Paragraph(apply_emoji_font(item, selected_font_name), item_style))
|
362 |
|
363 |
-
story.append(
|
364 |
|
365 |
# Page 2
|
366 |
story.append(Paragraph(apply_emoji_font("Deities Guide: Continued", selected_font_name), title_style))
|
|
|
10 |
import fitz # PyMuPDF
|
11 |
|
12 |
from reportlab.lib.pagesizes import A4
|
13 |
+
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle, PageBreak
|
14 |
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
15 |
from reportlab.lib import colors
|
16 |
from reportlab.pdfbase import pdfmetrics
|
|
|
160 |
14. **Hindu: Avatars & Devis** πΊ
|
161 |
1. Krishna, Rama, Durga: Sons and fierce mother.
|
162 |
2. Relation: Avatars and goddess protect dharma.
|
163 |
+
3. Lesson: Duty defeats evil.
|
164 |
15. **Japanese** πΈ
|
165 |
1. Amaterasu, Susanoo, Tsukuyomi: Sister, brothers.
|
166 |
2. Relation: Siblings balance cosmos.
|
|
|
216 |
"""
|
217 |
|
218 |
# ---------------------------------------------------------------
|
219 |
+
# Process markdown into a two-page layout based on line count.
|
220 |
def markdown_to_pdf_content(markdown_text):
|
221 |
lines = markdown_text.strip().split('\n')
|
222 |
pdf_content = []
|
|
|
261 |
if current_item and sub_items:
|
262 |
pdf_content.append([current_item, sub_items])
|
263 |
|
264 |
+
# Split based on line count, erring on left side longer.
|
265 |
+
total_lines = sum(1 + len(subs) if isinstance(item, list) else 1 for item in pdf_content)
|
266 |
+
target_lines = total_lines // 2
|
267 |
page1_content = []
|
268 |
page2_content = []
|
269 |
+
current_lines = 0
|
270 |
|
271 |
for item in pdf_content:
|
272 |
+
line_count = 1 + (len(item[1]) if isinstance(item, list) else 0)
|
273 |
+
if current_lines <= target_lines or len(page1_content) == 0:
|
274 |
page1_content.append(item)
|
275 |
+
current_lines += line_count
|
276 |
else:
|
277 |
page2_content.append(item)
|
278 |
|
|
|
360 |
else:
|
361 |
story.append(Paragraph(apply_emoji_font(item, selected_font_name), item_style))
|
362 |
|
363 |
+
story.append(PageBreak()) # Explicitly move to Page 2.
|
364 |
|
365 |
# Page 2
|
366 |
story.append(Paragraph(apply_emoji_font("Deities Guide: Continued", selected_font_name), title_style))
|