com3dian commited on
Commit
2341ee3
·
verified ·
1 Parent(s): 62a27ba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -23
app.py CHANGED
@@ -5,8 +5,9 @@ import os
5
  import pickle
6
  import torch
7
  import markdown
8
- from xhtml2pdf import pisa
9
  import io
 
10
  from grobidmonkey import reader
11
 
12
  from transformers import pipeline
@@ -175,29 +176,21 @@ def create_pdf_from_markdown_strings(markdown_strings):
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
-
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
 
 
5
  import pickle
6
  import torch
7
  import markdown
8
+ from weasyprint import HTML
9
  import io
10
+ from io import BytesIO
11
  from grobidmonkey import reader
12
 
13
  from transformers import pipeline
 
176
 
177
  return html_pages
178
 
179
+ def generate_pdf(html):
180
+ # Create a PDF from the HTML string
181
+ pdf_file = BytesIO()
182
+ HTML(string=html).write_pdf(pdf_file)
183
+ pdf_file.seek(0)
184
+ return pdf_file
 
 
185
 
186
  html_content = create_pdf_from_markdown_strings(st.session_state.summ_text)
187
+ pdf_file = generate_pdf(html_content)
 
 
 
188
 
189
+ # Provide download link
190
+ st.download_button(
191
+ label="Download PDF",
192
+ data=pdf_file,
193
+ file_name="sample.pdf",
194
+ mime="application/pdf"
195
+ )
 
 
 
196