Update app.py
Browse files
app.py
CHANGED
|
@@ -27,14 +27,11 @@ def create_pdf_tab(default_markdown):
|
|
| 27 |
with st.sidebar:
|
| 28 |
selected_font_name = st.selectbox("Select NotoEmoji Font", options=list(available_fonts.keys()))
|
| 29 |
selected_font_path = available_fonts[selected_font_name]
|
| 30 |
-
|
| 31 |
-
base_font_size = st.slider("Base Font Size (points)", min_value=6, max_value=16, value=9, step=1) # Default to 9
|
| 32 |
-
if auto_size:
|
| 33 |
-
st.info("Font size will adjust between 6-16 points to fit content on one page, starting from your base size.")
|
| 34 |
plain_text_mode = st.checkbox("Render as Plain Text (Preserve Bold Only)", value=False)
|
| 35 |
-
num_columns = st.selectbox("Number of Columns", options=[1, 2, 3, 4, 5, 6], index=3) # Default to 4
|
| 36 |
-
|
| 37 |
-
#
|
| 38 |
if 'markdown_content' not in st.session_state:
|
| 39 |
st.session_state.markdown_content = default_markdown
|
| 40 |
|
|
@@ -98,7 +95,7 @@ def create_pdf_tab(default_markdown):
|
|
| 98 |
return pdf_content, total_lines
|
| 99 |
|
| 100 |
# Create PDF
|
| 101 |
-
def create_pdf(markdown_text, base_font_size,
|
| 102 |
buffer = io.BytesIO()
|
| 103 |
# Double A4 page: A4 width * 2 (landscape)
|
| 104 |
page_width = A4[0] * 2
|
|
@@ -109,17 +106,6 @@ def create_pdf_tab(default_markdown):
|
|
| 109 |
spacer_height = 10
|
| 110 |
pdf_content, total_lines = markdown_to_pdf_content(markdown_text, plain_text_mode)
|
| 111 |
|
| 112 |
-
# Auto-size logic: reduce font size as content grows to fit one page
|
| 113 |
-
if auto_size and total_lines > 0:
|
| 114 |
-
# Target fitting content within page height (accounting for margins and spacing)
|
| 115 |
-
target_lines_per_column = (page_height - 72 - spacer_height) / (base_font_size * 1.15) # Approx lines per column
|
| 116 |
-
total_target_lines = target_lines_per_column * num_columns
|
| 117 |
-
if total_lines > total_target_lines:
|
| 118 |
-
scale_factor = total_target_lines / total_lines
|
| 119 |
-
base_font_size = max(6, min(16, base_font_size * scale_factor))
|
| 120 |
-
else:
|
| 121 |
-
base_font_size = min(16, base_font_size * (total_lines / total_target_lines))
|
| 122 |
-
|
| 123 |
item_font_size = base_font_size
|
| 124 |
section_font_size = base_font_size * 1.1
|
| 125 |
|
|
@@ -191,7 +177,7 @@ def create_pdf_tab(default_markdown):
|
|
| 191 |
|
| 192 |
# Main logic (PDF generation and preview only)
|
| 193 |
with st.spinner("Generating PDF..."):
|
| 194 |
-
pdf_bytes = create_pdf(st.session_state.markdown_content, base_font_size,
|
| 195 |
|
| 196 |
with st.container():
|
| 197 |
pdf_images = pdf_to_image(pdf_bytes)
|
|
@@ -201,7 +187,9 @@ def create_pdf_tab(default_markdown):
|
|
| 201 |
else:
|
| 202 |
st.info("Download the PDF to view it locally.")
|
| 203 |
|
| 204 |
-
|
|
|
|
|
|
|
| 205 |
|
| 206 |
default_markdown = """# Deities Guide: Mythology and Moral Lessons ๐
|
| 207 |
|
|
|
|
| 27 |
with st.sidebar:
|
| 28 |
selected_font_name = st.selectbox("Select NotoEmoji Font", options=list(available_fonts.keys()))
|
| 29 |
selected_font_path = available_fonts[selected_font_name]
|
| 30 |
+
base_font_size = st.slider("Font Size (points)", min_value=6, max_value=16, value=9, step=1) # Default to 9
|
|
|
|
|
|
|
|
|
|
| 31 |
plain_text_mode = st.checkbox("Render as Plain Text (Preserve Bold Only)", value=False)
|
| 32 |
+
num_columns = st.selectbox("Number of Columns", options=[1, 2, 3, 4, 5, 6], index=3) # Default to 4
|
| 33 |
+
|
| 34 |
+
# Markdown editor and buttons
|
| 35 |
if 'markdown_content' not in st.session_state:
|
| 36 |
st.session_state.markdown_content = default_markdown
|
| 37 |
|
|
|
|
| 95 |
return pdf_content, total_lines
|
| 96 |
|
| 97 |
# Create PDF
|
| 98 |
+
def create_pdf(markdown_text, base_font_size, plain_text_mode, num_columns):
|
| 99 |
buffer = io.BytesIO()
|
| 100 |
# Double A4 page: A4 width * 2 (landscape)
|
| 101 |
page_width = A4[0] * 2
|
|
|
|
| 106 |
spacer_height = 10
|
| 107 |
pdf_content, total_lines = markdown_to_pdf_content(markdown_text, plain_text_mode)
|
| 108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
item_font_size = base_font_size
|
| 110 |
section_font_size = base_font_size * 1.1
|
| 111 |
|
|
|
|
| 177 |
|
| 178 |
# Main logic (PDF generation and preview only)
|
| 179 |
with st.spinner("Generating PDF..."):
|
| 180 |
+
pdf_bytes = create_pdf(st.session_state.markdown_content, base_font_size, plain_text_mode, num_columns)
|
| 181 |
|
| 182 |
with st.container():
|
| 183 |
pdf_images = pdf_to_image(pdf_bytes)
|
|
|
|
| 187 |
else:
|
| 188 |
st.info("Download the PDF to view it locally.")
|
| 189 |
|
| 190 |
+
# Moved "Download PDF" to sidebar
|
| 191 |
+
with st.sidebar:
|
| 192 |
+
st.download_button(label="Download PDF", data=pdf_bytes, file_name="deities_guide.pdf", mime="application/pdf")
|
| 193 |
|
| 194 |
default_markdown = """# Deities Guide: Mythology and Moral Lessons ๐
|
| 195 |
|