Spaces:
Runtime error
Runtime error
Commit
·
3549626
1
Parent(s):
ca03281
Update app.py
Browse files
app.py
CHANGED
@@ -77,11 +77,17 @@ def process_text(inputs):
|
|
77 |
|
78 |
|
79 |
def save_as_pdf(text):
|
80 |
-
#
|
81 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".html") as tmpfile:
|
82 |
-
|
83 |
-
tmpfile.
|
84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
|
86 |
# encode the PDF to base64
|
87 |
b64 = base64.b64encode(pdf_file).decode('utf-8')
|
@@ -92,6 +98,7 @@ def save_as_pdf(text):
|
|
92 |
|
93 |
|
94 |
|
|
|
95 |
def form_page():
|
96 |
|
97 |
st.markdown("### Your custom obituary writer :pencil:")
|
@@ -191,33 +198,46 @@ def editor_page():
|
|
191 |
quill_text = st.session_state.output_text
|
192 |
edited_text = st_quill(quill_text)
|
193 |
|
194 |
-
|
195 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
196 |
|
|
|
|
|
|
|
|
|
197 |
st.session_state.edited_text = edited_text
|
198 |
|
|
|
|
|
|
|
199 |
if st.button("Save as PDF"):
|
200 |
# Save the output text as a PDF
|
201 |
-
save_as_pdf(
|
202 |
st.write("The custom obituary has been saved as a PDF.")
|
203 |
|
204 |
# Add some custom CSS to style the editor
|
205 |
-
st.markdown("""
|
206 |
<style>
|
207 |
-
#toolbar {
|
208 |
background-color: #f3f3f3;
|
209 |
border-radius: 5px;
|
210 |
padding: 5px;
|
211 |
-
}
|
212 |
-
.ql-container {
|
213 |
border-radius: 5px;
|
214 |
border: 1px solid #ccc;
|
215 |
height: 400px;
|
216 |
-
}
|
217 |
-
.ql-editor {
|
218 |
height: 100%;
|
219 |
-
|
220 |
-
|
221 |
</style>
|
222 |
""", unsafe_allow_html=True)
|
223 |
|
|
|
77 |
|
78 |
|
79 |
def save_as_pdf(text):
|
80 |
+
# create a temporary file to write the HTML string
|
81 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".html") as tmpfile:
|
82 |
+
# write the HTML string to the temporary file
|
83 |
+
tmpfile.write(text.encode())
|
84 |
+
|
85 |
+
# create a PDF file using the temporary HTML file and the CSS styles
|
86 |
+
options = {
|
87 |
+
"quiet": "",
|
88 |
+
"enable-local-file-access": ""
|
89 |
+
}
|
90 |
+
pdf_file = pdfkit.from_file(tmpfile.name, False, options=options)
|
91 |
|
92 |
# encode the PDF to base64
|
93 |
b64 = base64.b64encode(pdf_file).decode('utf-8')
|
|
|
98 |
|
99 |
|
100 |
|
101 |
+
|
102 |
def form_page():
|
103 |
|
104 |
st.markdown("### Your custom obituary writer :pencil:")
|
|
|
198 |
quill_text = st.session_state.output_text
|
199 |
edited_text = st_quill(quill_text)
|
200 |
|
201 |
+
# get the CSS styles from the edited text
|
202 |
+
css = edited_text.get("css")
|
203 |
+
css += """
|
204 |
+
.ql-editor {
|
205 |
+
font-size: 14px;
|
206 |
+
line-height: 1.5;
|
207 |
+
}
|
208 |
+
"""
|
209 |
|
210 |
+
st.write("Here is the edited obituary:")
|
211 |
+
st.markdown(edited_text.get("html"), unsafe_allow_html=True)
|
212 |
+
|
213 |
+
#st.write(edited_text)
|
214 |
st.session_state.edited_text = edited_text
|
215 |
|
216 |
+
|
217 |
+
#st.session_state.edited_text = edited_text
|
218 |
+
|
219 |
if st.button("Save as PDF"):
|
220 |
# Save the output text as a PDF
|
221 |
+
save_as_pdf(edited_text.get("html") + f"<style>{css}</style>")
|
222 |
st.write("The custom obituary has been saved as a PDF.")
|
223 |
|
224 |
# Add some custom CSS to style the editor
|
225 |
+
st.markdown(f"""
|
226 |
<style>
|
227 |
+
#toolbar {{
|
228 |
background-color: #f3f3f3;
|
229 |
border-radius: 5px;
|
230 |
padding: 5px;
|
231 |
+
}}
|
232 |
+
.ql-container {{
|
233 |
border-radius: 5px;
|
234 |
border: 1px solid #ccc;
|
235 |
height: 400px;
|
236 |
+
}}
|
237 |
+
.ql-editor {{
|
238 |
height: 100%;
|
239 |
+
{css}
|
240 |
+
}}
|
241 |
</style>
|
242 |
""", unsafe_allow_html=True)
|
243 |
|