Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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, font_size=10):
|
| 224 |
lines = markdown_text.strip().split('\n')
|
| 225 |
pdf_content = []
|
| 226 |
|
|
@@ -287,22 +293,8 @@ def markdown_to_pdf_content(markdown_text, show_subitems=True, plain_text_mode=F
|
|
| 287 |
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)
|
| 288 |
avg_line_length = total_chars / total_lines if total_lines > 0 else 1
|
| 289 |
|
| 290 |
-
page_height = A4[0] - 72
|
| 291 |
-
page_width = A4[1] * 2 - 72
|
| 292 |
-
line_height = font_size * 1.15
|
| 293 |
-
lines_per_column = page_height / line_height
|
| 294 |
-
|
| 295 |
-
chars_per_line = avg_line_length * (font_size / 10)
|
| 296 |
-
lines_per_column_width = page_width / (chars_per_line * 0.1)
|
| 297 |
-
|
| 298 |
-
num_columns = max(2, int(total_lines / lines_per_column))
|
| 299 |
-
num_columns = min(num_columns, int(lines_per_column_width))
|
| 300 |
-
if num_columns % 2 != 0:
|
| 301 |
-
num_columns += 1
|
| 302 |
-
num_columns = min(num_columns, 6)
|
| 303 |
-
|
| 304 |
-
lines_per_column = total_lines / num_columns
|
| 305 |
columns = [[] for _ in range(num_columns)]
|
|
|
|
| 306 |
current_line_count = 0
|
| 307 |
current_column = 0
|
| 308 |
|
|
@@ -316,7 +308,7 @@ def markdown_to_pdf_content(markdown_text, show_subitems=True, plain_text_mode=F
|
|
| 316 |
|
| 317 |
return columns, total_lines, num_columns
|
| 318 |
|
| 319 |
-
def create_main_pdf(markdown_text, base_font_size=10, auto_size=False, show_subitems=True, plain_text_mode=False):
|
| 320 |
buffer = io.BytesIO()
|
| 321 |
doc = SimpleDocTemplate(
|
| 322 |
buffer,
|
|
@@ -330,7 +322,7 @@ def create_main_pdf(markdown_text, base_font_size=10, auto_size=False, show_subi
|
|
| 330 |
styles = getSampleStyleSheet()
|
| 331 |
story = []
|
| 332 |
spacer_height = 10
|
| 333 |
-
columns, total_lines, num_columns = markdown_to_pdf_content(markdown_text, show_subitems, plain_text_mode, base_font_size)
|
| 334 |
|
| 335 |
if auto_size:
|
| 336 |
base_font_size = max(6, min(16, base_font_size * 300 / total_lines))
|
|
@@ -441,7 +433,7 @@ if 'markdown_content' not in st.session_state:
|
|
| 441 |
st.session_state.markdown_content = default_markdown
|
| 442 |
|
| 443 |
with st.spinner("Generating PDF..."):
|
| 444 |
-
pdf_bytes = create_main_pdf(st.session_state.markdown_content, base_font_size, auto_size, show_subitems, plain_text_mode)
|
| 445 |
|
| 446 |
with st.container():
|
| 447 |
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 |
|
|
|
|
| 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)
|