com3dian commited on
Commit
45e4e41
·
verified ·
1 Parent(s): b0eedcf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -2
app.py CHANGED
@@ -145,7 +145,7 @@ st.markdown(st.session_state.current_text)
145
  def render_markdown_to_html(markdown_str):
146
  return markdown.markdown(markdown_str)
147
 
148
- def create_pdf_from_markdown_strings(markdown_strings, output_file):
149
  html_pages = [render_markdown_to_html(md) for md in markdown_strings]
150
 
151
  # Combine HTML content with page breaks and add a style section for font size and margins
@@ -173,8 +173,31 @@ def create_pdf_from_markdown_strings(markdown_strings, output_file):
173
  'orientation': 'Landscape'
174
  }
175
 
 
 
 
 
 
 
 
 
176
  # Convert combined HTML to PDF directly into a file
177
  pdfkit.from_string(combined_html, output_file, options=options)
178
 
179
- create_pdf_from_markdown_strings(st.session_state.summ_text, '/tmp/output.pdf')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
180
 
 
145
  def render_markdown_to_html(markdown_str):
146
  return markdown.markdown(markdown_str)
147
 
148
+ def create_pdf_from_markdown_strings(markdown_strings):
149
  html_pages = [render_markdown_to_html(md) for md in markdown_strings]
150
 
151
  # Combine HTML content with page breaks and add a style section for font size and margins
 
173
  'orientation': 'Landscape'
174
  }
175
 
176
+ return html_pages
177
+
178
+ def html_to_pdf(html_content):
179
+ # Create a temporary file
180
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
181
+ pdfkit.from_string(html_content, tmp_file.name)
182
+ return tmp_file.name
183
+
184
  # Convert combined HTML to PDF directly into a file
185
  pdfkit.from_string(combined_html, output_file, options=options)
186
 
187
+ html_content = create_pdf_from_markdown_strings(st.session_state.summ_text)
188
+
189
+ if st.button("Download PDF"):
190
+ if html_content:
191
+ pdf_path = html_to_pdf(html_content)
192
+
193
+ with open(pdf_path, "rb") as pdf_file:
194
+ st.download_button(
195
+ label="Download PDF",
196
+ data=pdf_file,
197
+ file_name="converted.pdf",
198
+ mime="application/pdf"
199
+ )
200
+
201
+ # Remove the temporary file
202
+ os.remove(pdf_path)
203