codelion commited on
Commit
0d53c5f
·
verified ·
1 Parent(s): 76c3e8b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -48
app.py CHANGED
@@ -29,57 +29,60 @@ def convert_markdown_to_pdf(file_obj):
29
  extensions=['extra', 'codehilite']
30
  )
31
 
32
- # Wrap HTML content with proper HTML structure and CSS
33
- full_html = f"""
34
- <!DOCTYPE html>
35
- <html>
36
- <head>
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
- }}
49
- h1 {{ color: #2c3e50; margin-top: 1em; }}
50
- h2 {{ color: #34495e; margin-top: 0.8em; }}
51
- code {{
52
- background-color: #f7f7f7;
53
- padding: 2px 5px;
54
- border-radius: 3px;
55
- font-family: monospace;
56
- }}
57
- pre {{
58
- background-color: #f7f7f7;
59
- padding: 15px;
60
- border-radius: 5px;
61
- white-space: pre-wrap;
62
- font-family: monospace;
63
- }}
64
- p {{
65
- margin-bottom: 1em;
66
- }}
67
- </style>
68
- </head>
69
- <body>
70
- {html_content}
71
- </body>
72
- </html>
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}")
85
  if 'pdf_path' in locals() and os.path.exists(pdf_path):
 
29
  extensions=['extra', 'codehilite']
30
  )
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  # Create temporary file for PDF
33
  pdf_path = tempfile.mktemp(suffix='.pdf')
 
 
 
 
34
 
35
+ # Define styles
36
+ styles = '''
37
+ @page {
38
+ size: A4;
39
+ margin: 2.5cm;
40
+ }
41
+ body {
42
+ font-family: Arial, sans-serif;
43
+ line-height: 1.6;
44
+ margin: 0 auto;
45
+ padding: 20px;
46
+ }
47
+ h1 { color: #2c3e50; margin-top: 1em; }
48
+ h2 { color: #34495e; margin-top: 0.8em; }
49
+ code {
50
+ background-color: #f7f7f7;
51
+ padding: 2px 5px;
52
+ border-radius: 3px;
53
+ font-family: monospace;
54
+ }
55
+ pre {
56
+ background-color: #f7f7f7;
57
+ padding: 15px;
58
+ border-radius: 5px;
59
+ white-space: pre-wrap;
60
+ font-family: monospace;
61
+ }
62
+ p {
63
+ margin-bottom: 1em;
64
+ }
65
+ '''
66
+
67
+ # Create HTML document
68
+ html = HTML(string=f'''
69
+ <!DOCTYPE html>
70
+ <html>
71
+ <head>
72
+ <meta charset="UTF-8">
73
+ <style>{styles}</style>
74
+ </head>
75
+ <body>
76
+ {html_content}
77
+ </body>
78
+ </html>
79
+ ''')
80
+
81
+ # Render PDF
82
+ document = html.render()
83
+ document.write_pdf(target=pdf_path)
84
+
85
+ return html_content, pdf_path
86
  except Exception as e:
87
  print(f"Error converting to PDF: {e}")
88
  if 'pdf_path' in locals() and os.path.exists(pdf_path):