codelion commited on
Commit
2a53c81
·
verified ·
1 Parent(s): 219e60c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -7
app.py CHANGED
@@ -3,7 +3,7 @@ import gradio as gr
3
  import markdown
4
  import tempfile
5
  import os
6
- from weasyprint import HTML
7
  from pathlib import Path
8
 
9
  def convert_markdown_to_pdf(file_obj):
@@ -24,7 +24,10 @@ def convert_markdown_to_pdf(file_obj):
24
  markdown_content = str(file_obj)
25
 
26
  # Convert markdown to HTML
27
- html_content = markdown.markdown(markdown_content)
 
 
 
28
 
29
  # Wrap HTML content with proper HTML structure and CSS
30
  full_html = f"""
@@ -34,12 +37,12 @@ def convert_markdown_to_pdf(file_obj):
34
  <meta charset="UTF-8">
35
  <style>
36
  @page {{
 
37
  margin: 2.5cm;
38
  }}
39
  body {{
40
  font-family: Arial, sans-serif;
41
  line-height: 1.6;
42
- max-width: 800px;
43
  margin: 0 auto;
44
  padding: 20px;
45
  }}
@@ -58,6 +61,9 @@ def convert_markdown_to_pdf(file_obj):
58
  white-space: pre-wrap;
59
  font-family: monospace;
60
  }}
 
 
 
61
  </style>
62
  </head>
63
  <body>
@@ -67,11 +73,12 @@ def convert_markdown_to_pdf(file_obj):
67
  """
68
 
69
  # Create temporary file for PDF
70
- with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as pdf_file:
71
- pdf_path = pdf_file.name
72
 
73
  # Convert HTML to PDF using WeasyPrint
74
- HTML(string=full_html).write_pdf(pdf_path)
 
 
75
  return full_html, pdf_path
76
  except Exception as e:
77
  print(f"Error converting to PDF: {e}")
@@ -112,4 +119,4 @@ with gr.Blocks() as app:
112
  )
113
 
114
  if __name__ == "__main__":
115
- app.launch(share=True) # Added share=True for public access
 
3
  import markdown
4
  import tempfile
5
  import os
6
+ from weasyprint import HTML, CSS
7
  from pathlib import Path
8
 
9
  def convert_markdown_to_pdf(file_obj):
 
24
  markdown_content = str(file_obj)
25
 
26
  # Convert markdown to HTML
27
+ html_content = markdown.markdown(
28
+ markdown_content,
29
+ extensions=['extra', 'codehilite']
30
+ )
31
 
32
  # Wrap HTML content with proper HTML structure and CSS
33
  full_html = f"""
 
37
  <meta charset="UTF-8">
38
  <style>
39
  @page {{
40
+ size: A4;
41
  margin: 2.5cm;
42
  }}
43
  body {{
44
  font-family: Arial, sans-serif;
45
  line-height: 1.6;
 
46
  margin: 0 auto;
47
  padding: 20px;
48
  }}
 
61
  white-space: pre-wrap;
62
  font-family: monospace;
63
  }}
64
+ p {{
65
+ margin-bottom: 1em;
66
+ }}
67
  </style>
68
  </head>
69
  <body>
 
73
  """
74
 
75
  # Create temporary file for PDF
76
+ pdf_path = tempfile.mktemp(suffix='.pdf')
 
77
 
78
  # Convert HTML to PDF using WeasyPrint
79
+ html = HTML(string=full_html)
80
+ html.write_pdf(pdf_path)
81
+
82
  return full_html, pdf_path
83
  except Exception as e:
84
  print(f"Error converting to PDF: {e}")
 
119
  )
120
 
121
  if __name__ == "__main__":
122
+ app.launch(share=True)