Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +48 -37
src/streamlit_app.py
CHANGED
|
@@ -20,8 +20,9 @@ def convert_html_to_pdf(html_content, aspect_ratio):
|
|
| 20 |
aspect_ratio: One of "16:9", "1:1", or "9:16"
|
| 21 |
|
| 22 |
Returns:
|
| 23 |
-
|
| 24 |
"""
|
|
|
|
| 25 |
try:
|
| 26 |
# Create temporary directory for processing
|
| 27 |
temp_dir = tempfile.mkdtemp()
|
|
@@ -31,12 +32,17 @@ def convert_html_to_pdf(html_content, aspect_ratio):
|
|
| 31 |
with open(html_file, 'w', encoding='utf-8') as f:
|
| 32 |
f.write(html_content)
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
# Run Node.js script to convert HTML to PDF
|
| 35 |
result = subprocess.run(
|
| 36 |
-
['node',
|
| 37 |
capture_output=True,
|
| 38 |
text=True,
|
| 39 |
-
timeout=60
|
|
|
|
| 40 |
)
|
| 41 |
|
| 42 |
if result.returncode != 0:
|
|
@@ -48,18 +54,22 @@ def convert_html_to_pdf(html_content, aspect_ratio):
|
|
| 48 |
if not os.path.exists(pdf_file):
|
| 49 |
return None, "PDF file was not generated"
|
| 50 |
|
| 51 |
-
#
|
| 52 |
-
|
| 53 |
-
|
| 54 |
|
| 55 |
# Clean up temporary directory
|
| 56 |
shutil.rmtree(temp_dir, ignore_errors=True)
|
| 57 |
|
| 58 |
-
return
|
| 59 |
|
| 60 |
except subprocess.TimeoutExpired:
|
|
|
|
|
|
|
| 61 |
return None, "Error: PDF conversion timed out (60 seconds)"
|
| 62 |
except Exception as e:
|
|
|
|
|
|
|
| 63 |
return None, f"Error: {str(e)}"
|
| 64 |
|
| 65 |
# Page header
|
|
@@ -80,7 +90,8 @@ with tab1:
|
|
| 80 |
uploaded_file = st.file_uploader(
|
| 81 |
"Choose an HTML file",
|
| 82 |
type=['html', 'htm'],
|
| 83 |
-
key="file_uploader"
|
|
|
|
| 84 |
)
|
| 85 |
|
| 86 |
aspect_ratio_file = st.radio(
|
|
@@ -96,30 +107,34 @@ with tab1:
|
|
| 96 |
with col2:
|
| 97 |
if convert_file_btn:
|
| 98 |
if uploaded_file is not None:
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
else:
|
| 109 |
-
st.success("✅ PDF generated successfully!")
|
| 110 |
|
| 111 |
-
#
|
| 112 |
-
|
| 113 |
-
pdf_data = f.read()
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
else:
|
| 124 |
st.warning("Please upload an HTML file first.")
|
| 125 |
|
|
@@ -176,21 +191,17 @@ with tab2:
|
|
| 176 |
if html_code and html_code.strip():
|
| 177 |
with st.spinner("Converting HTML to PDF..."):
|
| 178 |
# Convert to PDF
|
| 179 |
-
|
| 180 |
|
| 181 |
if error:
|
| 182 |
st.error(error)
|
| 183 |
else:
|
| 184 |
st.success("✅ PDF generated successfully!")
|
| 185 |
|
| 186 |
-
#
|
| 187 |
-
with open(pdf_path, 'rb') as f:
|
| 188 |
-
pdf_data = f.read()
|
| 189 |
-
|
| 190 |
-
# Download button
|
| 191 |
st.download_button(
|
| 192 |
label="⬇️ Download PDF",
|
| 193 |
-
data=
|
| 194 |
file_name="converted.pdf",
|
| 195 |
mime="application/pdf",
|
| 196 |
use_container_width=True
|
|
|
|
| 20 |
aspect_ratio: One of "16:9", "1:1", or "9:16"
|
| 21 |
|
| 22 |
Returns:
|
| 23 |
+
Tuple of (pdf_bytes, error_message)
|
| 24 |
"""
|
| 25 |
+
temp_dir = None
|
| 26 |
try:
|
| 27 |
# Create temporary directory for processing
|
| 28 |
temp_dir = tempfile.mkdtemp()
|
|
|
|
| 32 |
with open(html_file, 'w', encoding='utf-8') as f:
|
| 33 |
f.write(html_content)
|
| 34 |
|
| 35 |
+
# Get the path to puppeteer_pdf.js (it's in parent directory)
|
| 36 |
+
script_dir = os.path.dirname(os.path.abspath(__file__))
|
| 37 |
+
puppeteer_script = os.path.join(os.path.dirname(script_dir), 'puppeteer_pdf.js')
|
| 38 |
+
|
| 39 |
# Run Node.js script to convert HTML to PDF
|
| 40 |
result = subprocess.run(
|
| 41 |
+
['node', puppeteer_script, html_file, aspect_ratio],
|
| 42 |
capture_output=True,
|
| 43 |
text=True,
|
| 44 |
+
timeout=60,
|
| 45 |
+
cwd=os.path.dirname(script_dir)
|
| 46 |
)
|
| 47 |
|
| 48 |
if result.returncode != 0:
|
|
|
|
| 54 |
if not os.path.exists(pdf_file):
|
| 55 |
return None, "PDF file was not generated"
|
| 56 |
|
| 57 |
+
# Read PDF file into memory
|
| 58 |
+
with open(pdf_file, 'rb') as f:
|
| 59 |
+
pdf_bytes = f.read()
|
| 60 |
|
| 61 |
# Clean up temporary directory
|
| 62 |
shutil.rmtree(temp_dir, ignore_errors=True)
|
| 63 |
|
| 64 |
+
return pdf_bytes, None
|
| 65 |
|
| 66 |
except subprocess.TimeoutExpired:
|
| 67 |
+
if temp_dir:
|
| 68 |
+
shutil.rmtree(temp_dir, ignore_errors=True)
|
| 69 |
return None, "Error: PDF conversion timed out (60 seconds)"
|
| 70 |
except Exception as e:
|
| 71 |
+
if temp_dir:
|
| 72 |
+
shutil.rmtree(temp_dir, ignore_errors=True)
|
| 73 |
return None, f"Error: {str(e)}"
|
| 74 |
|
| 75 |
# Page header
|
|
|
|
| 90 |
uploaded_file = st.file_uploader(
|
| 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(
|
|
|
|
| 107 |
with col2:
|
| 108 |
if convert_file_btn:
|
| 109 |
if uploaded_file is not None:
|
| 110 |
+
try:
|
| 111 |
+
with st.spinner("Converting HTML to PDF..."):
|
| 112 |
+
# Read uploaded file with proper encoding handling
|
| 113 |
+
try:
|
| 114 |
+
html_content = uploaded_file.read().decode('utf-8')
|
| 115 |
+
except UnicodeDecodeError:
|
| 116 |
+
# Try with latin-1 encoding if utf-8 fails
|
| 117 |
+
uploaded_file.seek(0)
|
| 118 |
+
html_content = uploaded_file.read().decode('latin-1')
|
|
|
|
|
|
|
| 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=f"{uploaded_file.name.replace('.html', '.pdf').replace('.htm', '.pdf')}",
|
| 133 |
+
mime="application/pdf",
|
| 134 |
+
use_container_width=True
|
| 135 |
+
)
|
| 136 |
+
except Exception as e:
|
| 137 |
+
st.error(f"Error processing file: {str(e)}")
|
| 138 |
else:
|
| 139 |
st.warning("Please upload an HTML file first.")
|
| 140 |
|
|
|
|
| 191 |
if html_code and html_code.strip():
|
| 192 |
with st.spinner("Converting HTML to PDF..."):
|
| 193 |
# Convert to PDF
|
| 194 |
+
pdf_bytes, error = convert_html_to_pdf(html_code, aspect_ratio_text)
|
| 195 |
|
| 196 |
if error:
|
| 197 |
st.error(error)
|
| 198 |
else:
|
| 199 |
st.success("✅ PDF generated successfully!")
|
| 200 |
|
| 201 |
+
# Download button with PDF bytes
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
st.download_button(
|
| 203 |
label="⬇️ Download PDF",
|
| 204 |
+
data=pdf_bytes,
|
| 205 |
file_name="converted.pdf",
|
| 206 |
mime="application/pdf",
|
| 207 |
use_container_width=True
|