amoldwalunj commited on
Commit
610f8bb
·
1 Parent(s): 55a5ef5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -6
app.py CHANGED
@@ -47,19 +47,25 @@ def process_text(inputs):
47
  # href = f'<a href="data:application/octet-stream;base64,{b64}" download="{file_name}">Download PDF</a>'
48
  # st.markdown(href, unsafe_allow_html=True)
49
 
 
 
50
  import base64
51
  from io import BytesIO
 
52
  from weasyprint import HTML
53
 
54
  def save_as_pdf(text):
55
- # convert quill editor HTML to PDF
56
- pdf_file = BytesIO()
57
- HTML(string=text).write_pdf(pdf_file)
58
-
59
- # encode the PDF to base64
 
 
 
60
  b64 = base64.b64encode(pdf_file.getvalue()).decode('utf-8')
61
 
62
- # generate a download link for the PDF
63
  href = f'<a href="data:application/octet-stream;base64,{b64}" download="output.pdf">Download PDF</a>'
64
  st.markdown(href, unsafe_allow_html=True)
65
 
 
47
  # href = f'<a href="data:application/octet-stream;base64,{b64}" download="{file_name}">Download PDF</a>'
48
  # st.markdown(href, unsafe_allow_html=True)
49
 
50
+
51
+
52
  import base64
53
  from io import BytesIO
54
+ import tempfile
55
  from weasyprint import HTML
56
 
57
  def save_as_pdf(text):
58
+ # Convert quill editor HTML to PDF
59
+ with tempfile.NamedTemporaryFile(mode="w+", suffix=".html") as html_file:
60
+ html_file.write(text)
61
+ html_file.seek(0)
62
+ pdf_file = BytesIO()
63
+ HTML(filename=html_file.name).write_pdf(pdf_file)
64
+
65
+ # Encode the PDF to base64
66
  b64 = base64.b64encode(pdf_file.getvalue()).decode('utf-8')
67
 
68
+ # Generate a download link for the PDF
69
  href = f'<a href="data:application/octet-stream;base64,{b64}" download="output.pdf">Download PDF</a>'
70
  st.markdown(href, unsafe_allow_html=True)
71