amoldwalunj commited on
Commit
f4e74e3
·
1 Parent(s): 4be8c4a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -30
app.py CHANGED
@@ -92,13 +92,8 @@ def save_as_pdf(html_string):
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
- # Read the PDF file contents and encode as a base64 string
96
- pdf_contents = pdffile.read()
97
- b64_pdf = base64.b64encode(pdf_contents).decode()
98
- # Generate download link for the PDF file
99
- href = f'<a href="data:application/octet-stream;base64,{b64_pdf}" download="document.pdf">Download PDF</a>'
100
- # Display the download link using st.write
101
- st.write(href, unsafe_allow_html=True)
102
 
103
 
104
 
@@ -207,29 +202,14 @@ def editor_page():
207
 
208
  st.session_state.edited_text = edited_text
209
 
210
- if st.button("Save as PDF"):
211
- # Save the output text as a PDF
212
- save_as_pdf(f"<html><head><style>{edited_text}</style></head><body>{edited_text}</body></html>")
213
- st.write("The custom obituary has been saved as a PDF.")
214
-
215
- # Add some custom CSS to style the editor
216
- st.markdown("""
217
- <style>
218
- #toolbar {
219
- background-color: #f3f3f3;
220
- border-radius: 5px;
221
- padding: 5px;
222
- }
223
- .ql-container {
224
- border-radius: 5px;
225
- border: 1px solid #ccc;
226
- height: 400px;
227
- }
228
- .ql-editor {
229
- height: 100%;
230
- }
231
- </style>
232
- """, unsafe_allow_html=True)
233
 
234
 
235
 
 
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
 
 
202
 
203
  st.session_state.edited_text = edited_text
204
 
205
+ if st.button("Download as PDF"):
206
+ # Save edited text as HTML string
207
+ html_string = f"<html><head><style>{edited_text.get('css', '')}</style></head><body>{edited_text.get('content', '')}</body></html>"
208
+ # Save HTML string as PDF file and generate download link
209
+ with st.spinner("Generating PDF..."):
210
+ pdf_file = save_as_pdf(html_string)
211
+ st.markdown(f'<a href="{pdf_file}" download="edited_text.pdf">Download PDF</a>', unsafe_allow_html=True)
212
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
213
 
214
 
215