File size: 1,123 Bytes
30fab6d
144a10c
93debc4
b509462
30fab6d
144a10c
bc4b931
30fab6d
144a10c
 
30fab6d
bc4b931
93debc4
144a10c
bc4b931
144a10c
93debc4
 
 
 
b509462
93debc4
b509462
 
 
 
 
 
 
bc4b931
b509462
bc4b931
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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"
            )