Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +39 -21
src/streamlit_app.py
CHANGED
|
@@ -91,9 +91,14 @@ with tab1:
|
|
| 91 |
"Choose an HTML file",
|
| 92 |
type=['html', 'htm'],
|
| 93 |
key="file_uploader",
|
| 94 |
-
help="Upload an HTML file (max 200MB)"
|
|
|
|
| 95 |
)
|
| 96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
aspect_ratio_file = st.radio(
|
| 98 |
"Aspect Ratio",
|
| 99 |
options=["16:9", "1:1", "9:16"],
|
|
@@ -102,41 +107,54 @@ with tab1:
|
|
| 102 |
help="Select the page orientation and dimensions"
|
| 103 |
)
|
| 104 |
|
| 105 |
-
convert_file_btn = st.button("π Convert to PDF", key="convert_file", type="primary", use_container_width=True)
|
| 106 |
|
| 107 |
with col2:
|
| 108 |
-
if convert_file_btn:
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
# Convert to PDF
|
| 121 |
pdf_bytes, error = convert_html_to_pdf(html_content, aspect_ratio_file)
|
| 122 |
|
| 123 |
if error:
|
| 124 |
-
st.error(error)
|
|
|
|
|
|
|
| 125 |
else:
|
| 126 |
st.success("β
PDF generated successfully!")
|
| 127 |
|
| 128 |
# Download button with PDF bytes
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
st.download_button(
|
| 130 |
label="β¬οΈ Download PDF",
|
| 131 |
data=pdf_bytes,
|
| 132 |
-
file_name=
|
| 133 |
mime="application/pdf",
|
| 134 |
-
use_container_width=True
|
|
|
|
| 135 |
)
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
|
|
|
| 140 |
|
| 141 |
# Tab 2: Paste HTML Code
|
| 142 |
with tab2:
|
|
|
|
| 91 |
"Choose an HTML file",
|
| 92 |
type=['html', 'htm'],
|
| 93 |
key="file_uploader",
|
| 94 |
+
help="Upload an HTML file (max 200MB)",
|
| 95 |
+
accept_multiple_files=False
|
| 96 |
)
|
| 97 |
|
| 98 |
+
# Display file info if uploaded
|
| 99 |
+
if uploaded_file is not None:
|
| 100 |
+
st.info(f"π File: {uploaded_file.name} ({uploaded_file.size} bytes)")
|
| 101 |
+
|
| 102 |
aspect_ratio_file = st.radio(
|
| 103 |
"Aspect Ratio",
|
| 104 |
options=["16:9", "1:1", "9:16"],
|
|
|
|
| 107 |
help="Select the page orientation and dimensions"
|
| 108 |
)
|
| 109 |
|
| 110 |
+
convert_file_btn = st.button("π Convert to PDF", key="convert_file", type="primary", use_container_width=True, disabled=(uploaded_file is None))
|
| 111 |
|
| 112 |
with col2:
|
| 113 |
+
if convert_file_btn and uploaded_file is not None:
|
| 114 |
+
try:
|
| 115 |
+
with st.spinner("Converting HTML to PDF..."):
|
| 116 |
+
# Reset file pointer to beginning
|
| 117 |
+
uploaded_file.seek(0)
|
| 118 |
+
|
| 119 |
+
# Read uploaded file with proper encoding handling
|
| 120 |
+
try:
|
| 121 |
+
html_content = uploaded_file.getvalue().decode('utf-8')
|
| 122 |
+
except UnicodeDecodeError:
|
| 123 |
+
# Try with latin-1 encoding if utf-8 fails
|
| 124 |
+
uploaded_file.seek(0)
|
| 125 |
+
html_content = uploaded_file.getvalue().decode('latin-1')
|
| 126 |
+
|
| 127 |
+
if not html_content or len(html_content.strip()) == 0:
|
| 128 |
+
st.error("The uploaded file is empty.")
|
| 129 |
+
else:
|
| 130 |
# Convert to PDF
|
| 131 |
pdf_bytes, error = convert_html_to_pdf(html_content, aspect_ratio_file)
|
| 132 |
|
| 133 |
if error:
|
| 134 |
+
st.error(f"β {error}")
|
| 135 |
+
with st.expander("Show error details"):
|
| 136 |
+
st.code(error)
|
| 137 |
else:
|
| 138 |
st.success("β
PDF generated successfully!")
|
| 139 |
|
| 140 |
# Download button with PDF bytes
|
| 141 |
+
output_filename = uploaded_file.name.replace('.html', '.pdf').replace('.htm', '.pdf')
|
| 142 |
+
if not output_filename.endswith('.pdf'):
|
| 143 |
+
output_filename += '.pdf'
|
| 144 |
+
|
| 145 |
st.download_button(
|
| 146 |
label="β¬οΈ Download PDF",
|
| 147 |
data=pdf_bytes,
|
| 148 |
+
file_name=output_filename,
|
| 149 |
mime="application/pdf",
|
| 150 |
+
use_container_width=True,
|
| 151 |
+
key="download_file_pdf"
|
| 152 |
)
|
| 153 |
+
except Exception as e:
|
| 154 |
+
st.error(f"β Error processing file: {str(e)}")
|
| 155 |
+
with st.expander("Show full error"):
|
| 156 |
+
import traceback
|
| 157 |
+
st.code(traceback.format_exc())
|
| 158 |
|
| 159 |
# Tab 2: Paste HTML Code
|
| 160 |
with tab2:
|