Spaces:
Runtime error
Runtime error
Commit
·
110c60a
1
Parent(s):
ee70b6e
Update app.py
Browse files
app.py
CHANGED
@@ -85,39 +85,21 @@ from bs4 import BeautifulSoup
|
|
85 |
import pdfkit
|
86 |
|
87 |
def save_as_pdf(text):
|
88 |
-
#
|
89 |
-
with open(
|
90 |
f.write(text)
|
91 |
|
92 |
-
#
|
93 |
-
|
94 |
-
"page-size": "Letter",
|
95 |
-
"margin-top": "0.75in",
|
96 |
-
"margin-right": "0.75in",
|
97 |
-
"margin-bottom": "0.75in",
|
98 |
-
"margin-left": "0.75in",
|
99 |
-
"encoding": "UTF-8",
|
100 |
-
"no-outline": None
|
101 |
-
}
|
102 |
-
|
103 |
-
# Generate the PDF from the HTML file
|
104 |
-
pdfkit.from_file("temp.html", "output.pdf", options=options)
|
105 |
-
|
106 |
-
# Read the generated PDF file
|
107 |
-
with open("output.pdf", "rb") as f:
|
108 |
-
pdf_data = f.read()
|
109 |
|
110 |
-
#
|
111 |
-
|
|
|
112 |
|
113 |
-
#
|
114 |
href = f'<a href="data:application/octet-stream;base64,{b64}" download="output.pdf">Download PDF</a>'
|
115 |
st.markdown(href, unsafe_allow_html=True)
|
116 |
|
117 |
-
# Delete the temporary HTML and PDF files
|
118 |
-
os.remove("temp.html")
|
119 |
-
os.remove("output.pdf")
|
120 |
-
|
121 |
|
122 |
|
123 |
|
@@ -221,14 +203,34 @@ def editor_page():
|
|
221 |
edited_text = st_quill(quill_text)
|
222 |
|
223 |
st.write("Here is the edited obituary:")
|
224 |
-
#st.write(edited_text)
|
225 |
|
226 |
-
st.session_state.edited_text = edited_text
|
227 |
|
228 |
if st.button("Save as PDF"):
|
229 |
-
# Save the
|
230 |
-
save_as_pdf(edited_text.get(
|
231 |
st.write("The custom obituary has been saved as a PDF.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
232 |
|
233 |
|
234 |
|
|
|
85 |
import pdfkit
|
86 |
|
87 |
def save_as_pdf(text):
|
88 |
+
# save the quill editor text to a temporary HTML file
|
89 |
+
with open('temp.html', 'w') as f:
|
90 |
f.write(text)
|
91 |
|
92 |
+
# convert the HTML file to a PDF with pdfkit
|
93 |
+
pdfkit.from_file('temp.html', 'output.pdf')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
|
95 |
+
# encode the PDF to base64
|
96 |
+
with open('output.pdf', 'rb') as f:
|
97 |
+
b64 = base64.b64encode(f.read()).decode('utf-8')
|
98 |
|
99 |
+
# generate a download link for the PDF
|
100 |
href = f'<a href="data:application/octet-stream;base64,{b64}" download="output.pdf">Download PDF</a>'
|
101 |
st.markdown(href, unsafe_allow_html=True)
|
102 |
|
|
|
|
|
|
|
|
|
103 |
|
104 |
|
105 |
|
|
|
203 |
edited_text = st_quill(quill_text)
|
204 |
|
205 |
st.write("Here is the edited obituary:")
|
|
|
206 |
|
207 |
+
st.session_state.edited_text = edited_text.get('html')
|
208 |
|
209 |
if st.button("Save as PDF"):
|
210 |
+
# Save the output text as a PDF
|
211 |
+
save_as_pdf(edited_text.get('html'))
|
212 |
st.write("The custom obituary has been saved as a PDF.")
|
213 |
+
|
214 |
+
# Add some custom CSS to style the editor
|
215 |
+
st.markdown("""
|
216 |
+
<style>
|
217 |
+
#toolbar {
|
218 |
+
background-color: #f3f3f3;
|
219 |
+
border-radius: 5px;
|
220 |
+
padding: 5px;
|
221 |
+
}
|
222 |
+
.ql-container {
|
223 |
+
border-radius: 5px;
|
224 |
+
border: 1px solid #ccc;
|
225 |
+
height: 400px;
|
226 |
+
}
|
227 |
+
.ql-editor {
|
228 |
+
height: 100%;
|
229 |
+
}
|
230 |
+
{edited_text.get('css')}
|
231 |
+
</style>
|
232 |
+
""", unsafe_allow_html=True)
|
233 |
+
|
234 |
|
235 |
|
236 |
|