amoldwalunj commited on
Commit
54b4c8b
1 Parent(s): 98a7c2c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -13
app.py CHANGED
@@ -95,18 +95,15 @@ def save_as_pdf(text):
95
  'margin-bottom': '0.75in',
96
  'margin-left': '0.75in',
97
  }
98
- try:
99
- pdfkit.from_string(text, pdf_file, options=options)
100
- except OSError as e:
101
- st.error("Error generating PDF. Please check if wkhtmltopdf is installed.")
102
- return
103
 
104
  # encode the PDF to base64
105
  b64 = base64.b64encode(pdf_file.getvalue()).decode('utf-8')
106
 
107
- # generate a download link for the PDF
108
- href = f'<a href="data:application/octet-stream;base64,{b64}" download="output.pdf">Download PDF</a>'
109
- st.markdown(href, unsafe_allow_html=True)
 
110
 
111
 
112
 
@@ -224,11 +221,15 @@ def editor_page():
224
  new_html = f"""<html><head><style>{css}</style></head><body>{soup}</body></html>"""
225
 
226
  # Save the output text as a PDF
227
- try:
228
- save_as_pdf(new_html)
229
- st.write("The custom obituary has been saved as a PDF.")
230
- except Exception as e:
231
- st.error("Error saving PDF. " + str(e))
 
 
 
 
232
 
233
  # Add some custom CSS to style the editor
234
  st.markdown("""
 
95
  'margin-bottom': '0.75in',
96
  'margin-left': '0.75in',
97
  }
98
+ pdfkit.from_string(text, pdf_file, options=options)
 
 
 
 
99
 
100
  # encode the PDF to base64
101
  b64 = base64.b64encode(pdf_file.getvalue()).decode('utf-8')
102
 
103
+ # generate the filename
104
+ filename = 'output.pdf'
105
+
106
+ return b64, filename
107
 
108
 
109
 
 
221
  new_html = f"""<html><head><style>{css}</style></head><body>{soup}</body></html>"""
222
 
223
  # Save the output text as a PDF
224
+ pdf_data, filename = save_as_pdf(new_html)
225
+
226
+ # display a download button for the PDF file
227
+ st.download_button(
228
+ label='Download PDF',
229
+ data=pdf_data,
230
+ file_name=filename,
231
+ mime='application/pdf'
232
+ )
233
 
234
  # Add some custom CSS to style the editor
235
  st.markdown("""