Spaces:
Runtime error
Runtime error
Commit
·
9849b14
1
Parent(s):
a3ed03d
Update app.py
Browse files
app.py
CHANGED
@@ -77,23 +77,23 @@ def process_text(inputs):
|
|
77 |
# st.markdown(href, unsafe_allow_html=True)
|
78 |
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
tmpfile.write(text.encode())
|
84 |
-
tmpfile.flush()
|
85 |
-
|
86 |
-
# generate a PDF from the HTML
|
87 |
-
pdf_file = BytesIO()
|
88 |
-
weasyprint.HTML(tmpfile.name).write_pdf(pdf_file)
|
89 |
-
|
90 |
-
# encode the PDF to base64
|
91 |
-
b64 = base64.b64encode(pdf_file.getvalue()).decode('utf-8')
|
92 |
-
|
93 |
-
# generate a download link for the PDF
|
94 |
-
href = f'<a href="data:application/octet-stream;base64,{b64}" download="output.pdf">Download PDF</a>'
|
95 |
-
st.markdown(href, unsafe_allow_html=True)
|
96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
|
99 |
|
|
|
77 |
# st.markdown(href, unsafe_allow_html=True)
|
78 |
|
79 |
|
80 |
+
import tempfile
|
81 |
+
import weasyprint
|
82 |
+
from bs4 import BeautifulSoup
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
|
84 |
+
def save_as_pdf(html_string):
|
85 |
+
# Parse HTML string into BeautifulSoup object
|
86 |
+
soup = BeautifulSoup(html_string, 'html.parser')
|
87 |
+
# Generate temporary file name with .html extension
|
88 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".html") as tmpfile:
|
89 |
+
# Write the prettified HTML string to the temporary file
|
90 |
+
tmpfile.write(soup.prettify().encode())
|
91 |
+
# Generate temporary file name with .pdf extension
|
92 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as pdffile:
|
93 |
+
# Convert the HTML file to a PDF file using WeasyPrint
|
94 |
+
weasyprint.HTML(tmpfile.name).write_pdf(pdffile)
|
95 |
+
# Return the file name of the PDF file
|
96 |
+
return pdffile.name
|
97 |
|
98 |
|
99 |
|