awacke1 commited on
Commit
ec4d47e
ยท
verified ยท
1 Parent(s): e6f9aea

Update backup1.app.py

Browse files
Files changed (1) hide show
  1. backup1.app.py +13 -11
backup1.app.py CHANGED
@@ -38,6 +38,12 @@ with st.sidebar:
38
 
39
  show_subitems = st.checkbox("Show Sub-items (e.g., 1.1, 2.1.3)", value=True)
40
  plain_text_mode = st.checkbox("Render as Plain Text (Preserve Bold Only)", value=False)
 
 
 
 
 
 
41
 
42
  pdfmetrics.registerFont(TTFont(selected_font_name, selected_font_path))
43
 
@@ -220,7 +226,7 @@ default_markdown = """# ๐ŸŒŸ Deities Guide: Mythology and Moral Lessons ๐ŸŒŸ
220
  3. Saints/Prophets: Virtues (e.g., justice, prophecy).
221
  """
222
 
223
- def markdown_to_pdf_content(markdown_text, show_subitems=True, plain_text_mode=False):
224
  lines = markdown_text.strip().split('\n')
225
  pdf_content = []
226
 
@@ -284,15 +290,11 @@ def markdown_to_pdf_content(markdown_text, show_subitems=True, plain_text_mode=F
284
  pdf_content.append([current_item, sub_items])
285
 
286
  total_lines = sum(1 + len(subs) if isinstance(item, list) else 1 for item in pdf_content)
287
- page_height = A4[0] - 72
288
- lines_per_column = total_lines / 2
289
- num_columns = max(2, int(total_lines / (page_height / (base_font_size * 1.15))))
290
- num_columns = min(num_columns, 4)
291
- if num_columns % 2 != 0:
292
- num_columns += 1
293
 
294
- lines_per_column = total_lines / num_columns
295
  columns = [[] for _ in range(num_columns)]
 
296
  current_line_count = 0
297
  current_column = 0
298
 
@@ -306,7 +308,7 @@ def markdown_to_pdf_content(markdown_text, show_subitems=True, plain_text_mode=F
306
 
307
  return columns, total_lines, num_columns
308
 
309
- def create_main_pdf(markdown_text, base_font_size=10, auto_size=False, show_subitems=True, plain_text_mode=False):
310
  buffer = io.BytesIO()
311
  doc = SimpleDocTemplate(
312
  buffer,
@@ -320,7 +322,7 @@ def create_main_pdf(markdown_text, base_font_size=10, auto_size=False, show_subi
320
  styles = getSampleStyleSheet()
321
  story = []
322
  spacer_height = 10
323
- columns, total_lines, num_columns = markdown_to_pdf_content(markdown_text, show_subitems, plain_text_mode)
324
 
325
  if auto_size:
326
  base_font_size = max(6, min(16, base_font_size * 300 / total_lines))
@@ -431,7 +433,7 @@ if 'markdown_content' not in st.session_state:
431
  st.session_state.markdown_content = default_markdown
432
 
433
  with st.spinner("Generating PDF..."):
434
- pdf_bytes = create_main_pdf(st.session_state.markdown_content, base_font_size, auto_size, show_subitems, plain_text_mode)
435
 
436
  with st.container():
437
  pdf_images = pdf_to_image(pdf_bytes)
 
38
 
39
  show_subitems = st.checkbox("Show Sub-items (e.g., 1.1, 2.1.3)", value=True)
40
  plain_text_mode = st.checkbox("Render as Plain Text (Preserve Bold Only)", value=False)
41
+
42
+ num_columns = st.radio(
43
+ "Number of Columns",
44
+ options=[1, 2, 3, 4, 5, 6],
45
+ index=3
46
+ )
47
 
48
  pdfmetrics.registerFont(TTFont(selected_font_name, selected_font_path))
49
 
 
226
  3. Saints/Prophets: Virtues (e.g., justice, prophecy).
227
  """
228
 
229
+ def markdown_to_pdf_content(markdown_text, show_subitems=True, plain_text_mode=False, font_size=10, num_columns=4):
230
  lines = markdown_text.strip().split('\n')
231
  pdf_content = []
232
 
 
290
  pdf_content.append([current_item, sub_items])
291
 
292
  total_lines = sum(1 + len(subs) if isinstance(item, list) else 1 for item in pdf_content)
293
+ total_chars = sum(len(item) if isinstance(item, str) else len(item[0]) + sum(len(sub) for sub in item[1]) for item in pdf_content)
294
+ avg_line_length = total_chars / total_lines if total_lines > 0 else 1
 
 
 
 
295
 
 
296
  columns = [[] for _ in range(num_columns)]
297
+ lines_per_column = total_lines / num_columns
298
  current_line_count = 0
299
  current_column = 0
300
 
 
308
 
309
  return columns, total_lines, num_columns
310
 
311
+ def create_main_pdf(markdown_text, base_font_size=10, auto_size=False, show_subitems=True, plain_text_mode=False, num_columns=4):
312
  buffer = io.BytesIO()
313
  doc = SimpleDocTemplate(
314
  buffer,
 
322
  styles = getSampleStyleSheet()
323
  story = []
324
  spacer_height = 10
325
+ columns, total_lines, num_columns = markdown_to_pdf_content(markdown_text, show_subitems, plain_text_mode, base_font_size, num_columns)
326
 
327
  if auto_size:
328
  base_font_size = max(6, min(16, base_font_size * 300 / total_lines))
 
433
  st.session_state.markdown_content = default_markdown
434
 
435
  with st.spinner("Generating PDF..."):
436
+ pdf_bytes = create_main_pdf(st.session_state.markdown_content, base_font_size, auto_size, show_subitems, plain_text_mode, num_columns)
437
 
438
  with st.container():
439
  pdf_images = pdf_to_image(pdf_bytes)