amoldwalunj commited on
Commit
8dcf8c0
1 Parent(s): 654a845

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -37
app.py CHANGED
@@ -82,19 +82,42 @@ import tempfile
82
  import weasyprint
83
  from bs4 import BeautifulSoup
84
 
85
- def save_as_pdf(html_string):
86
- # Parse HTML string into BeautifulSoup object
87
- soup = BeautifulSoup(html_string, 'html.parser')
88
- # Generate temporary file name with .html extension
89
- with tempfile.NamedTemporaryFile(delete=False, suffix=".html") as tmpfile:
90
- # Write the prettified HTML string to the temporary file
91
- tmpfile.write(soup.prettify().encode())
92
- # Generate temporary file name with .pdf extension
93
- with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as pdffile:
94
- # Convert the HTML file to a PDF file using WeasyPrint
95
- weasyprint.HTML(tmpfile.name).write_pdf(pdffile)
96
- # Return the file name of the PDF file
97
- return pdffile.name
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
 
99
 
100
 
@@ -187,20 +210,6 @@ def form_page():
187
 
188
  #from quill.delta import Delta, Renderer
189
 
190
- def st_quill(value):
191
- quill = st_quill_component(
192
- value=value,
193
- toolbar=[
194
- ["bold", "italic", "underline"],
195
- [{"list": "bullet"}],
196
- [{"align": ""}, {"align": "center"}, {"align": "right"}, {"align": "justify"}],
197
- ["link"],
198
- ],
199
- )
200
-
201
- edited_text = {"css": quill["css"], "content": quill["html"]}
202
-
203
- return edited_text
204
 
205
 
206
  def editor_page():
@@ -212,19 +221,14 @@ def editor_page():
212
  edited_text = st_quill(quill_text)
213
 
214
  st.write("Here is the edited obituary:")
 
215
 
216
  st.session_state.edited_text = edited_text
217
 
218
- if st.button("Download as PDF"):
219
- # Save edited text as HTML string
220
- html_string = f"<html><head><style>{edited_text['css']}</style></head><body>{edited_text['content']}</body></html>"
221
- # Save HTML string as PDF file and generate download link
222
- with st.spinner("Generating PDF..."):
223
- pdf_file = save_as_pdf(html_string)
224
- st.markdown(f'<a href="{pdf_file}" download="edited_text.pdf">Download PDF</a>', unsafe_allow_html=True)
225
-
226
-
227
-
228
 
229
 
230
 
 
82
  import weasyprint
83
  from bs4 import BeautifulSoup
84
 
85
+ import pdfkit
86
+
87
+ def save_as_pdf(text):
88
+ # Save the edited text as a temporary HTML file
89
+ with open("temp.html", "w", encoding="utf-8") as f:
90
+ f.write(text)
91
+
92
+ # Configure options for PDF generation
93
+ options = {
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
+ # Encode the PDF data to base64
111
+ b64 = base64.b64encode(pdf_data).decode("utf-8")
112
+
113
+ # Generate a download link for the PDF
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
 
 
210
 
211
  #from quill.delta import Delta, Renderer
212
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
213
 
214
 
215
  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 edited text as a PDF
230
+ save_as_pdf(edited_text.get("content"))
231
+ st.write("The custom obituary has been saved as a PDF.")
 
 
 
 
 
 
232
 
233
 
234