awacke1 commited on
Commit
c99c131
Β·
verified Β·
1 Parent(s): 7790e6c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -11
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
- Gabri 3. Lesson: Duty defeats evil.
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 for the PDF.
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 into two pages (roughly balanced).
265
- total_items = sum(1 + len(subs) if isinstance(item, list) else 1 for item in pdf_content)
266
- target_per_page = total_items // 2
267
  page1_content = []
268
  page2_content = []
269
- current_count = 0
270
 
271
  for item in pdf_content:
272
- item_count = 1 + (len(item[1]) if isinstance(item, list) else 0)
273
- if current_count < target_per_page or len(page1_content) == 0:
274
  page1_content.append(item)
275
- current_count += item_count
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(Spacer(1, A4[1] - sum(p.minHeight() for p in story) - 72)) # Fill page.
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))