Spaces:
Sleeping
Sleeping
import streamlit as st | |
from bs4 import BeautifulSoup | |
import pdfkit | |
import tempfile | |
# Set up Streamlit interface | |
st.title("HTML to PDF Converter") | |
# Input HTML content | |
html_input = st.text_area("Paste your HTML content here:", height=300) | |
if st.button("Convert to PDF"): | |
# Parse HTML content | |
soup = BeautifulSoup(html_input, "html.parser") | |
validated_html = str(soup) # Convert back to string if needed | |
# Specify the path to the wkhtmltopdf executable | |
config = pdfkit.configuration(wkhtmltopdf='/path/to/wkhtmltopdf') # Update this path based on your installation | |
# Convert HTML to PDF | |
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_pdf: | |
pdfkit.from_string(validated_html, tmp_pdf.name, configuration=config) | |
st.success("PDF generated successfully!") | |
# Provide a download button | |
with open(tmp_pdf.name, "rb") as pdf_file: | |
st.download_button( | |
label="Download PDF", | |
data=pdf_file, | |
file_name="converted_document.pdf", | |
mime="application/pdf" | |
) |