amoldwalunj commited on
Commit
bd95fa4
·
1 Parent(s): 110c60a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -11
app.py CHANGED
@@ -85,16 +85,19 @@ from bs4 import BeautifulSoup
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>'
@@ -202,13 +205,20 @@ def editor_page():
202
  quill_text = st.session_state.output_text
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
@@ -227,7 +237,6 @@ def editor_page():
227
  .ql-editor {
228
  height: 100%;
229
  }
230
- {edited_text.get('css')}
231
  </style>
232
  """, unsafe_allow_html=True)
233
 
 
85
  import pdfkit
86
 
87
  def save_as_pdf(text):
88
+ # convert quill editor HTML to PDF
89
+ pdf_file = BytesIO()
90
+ options = {
91
+ 'page-size': 'Letter',
92
+ 'margin-top': '0.75in',
93
+ 'margin-right': '0.75in',
94
+ 'margin-bottom': '0.75in',
95
+ 'margin-left': '0.75in',
96
+ }
97
+ pdfkit.from_string(text, pdf_file, options=options)
98
 
99
  # encode the PDF to base64
100
+ b64 = base64.b64encode(pdf_file.getvalue()).decode('utf-8')
 
101
 
102
  # generate a download link for the PDF
103
  href = f'<a href="data:application/octet-stream;base64,{b64}" download="output.pdf">Download PDF</a>'
 
205
  quill_text = st.session_state.output_text
206
  edited_text = st_quill(quill_text)
207
 
208
+ # Extract the HTML and CSS content from the quill editor
209
+ soup = BeautifulSoup(edited_text['html'], 'html.parser')
210
+ style_tag = soup.new_tag('style')
211
+ style_tag.string = edited_text['css']
212
+ soup.head.append(style_tag)
213
+ html_content = str(soup)
214
+
215
  st.write("Here is the edited obituary:")
216
 
217
+ st.session_state.edited_text = html_content
218
 
219
  if st.button("Save as PDF"):
220
  # Save the output text as a PDF
221
+ save_as_pdf(html_content)
222
  st.write("The custom obituary has been saved as a PDF.")
223
 
224
  # Add some custom CSS to style the editor
 
237
  .ql-editor {
238
  height: 100%;
239
  }
 
240
  </style>
241
  """, unsafe_allow_html=True)
242