-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_handler.py
More file actions
141 lines (133 loc) · 3.58 KB
/
api_handler.py
File metadata and controls
141 lines (133 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
"""
API handler for Markdown to PDF conversion.
"""
from pathlib import Path
from io import BytesIO
try:
import markdown
from weasyprint import HTML
LIBRARIES_AVAILABLE = True
except ImportError:
LIBRARIES_AVAILABLE = False
def convert_markdown_to_pdf(markdown_text: str, filename: str = "document") -> BytesIO:
"""
Convert Markdown text to PDF.
Args:
markdown_text: Markdown content as string
filename: Output filename (without extension)
Returns:
BytesIO object containing PDF data
"""
if not LIBRARIES_AVAILABLE:
raise ImportError("Required libraries not installed. Run: pip install -r requirements.txt")
# Convert Markdown to HTML
html_content = markdown.markdown(
markdown_text,
extensions=['extra', 'codehilite', 'tables', 'fenced_code']
)
# Read CSS from app/style.css
base_dir = Path(__file__).parent
css_path = base_dir / "app" / "style.css"
# Create full HTML document with styles
full_html = f"""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{filename}</title>
<style>
@page {{
size: A4;
margin: 20mm;
}}
body {{
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
line-height: 1.6;
color: #111827;
max-width: 900px;
margin: 0 auto;
padding: 24px;
background: #ffffff;
}}
h1, h2, h3 {{
color: #0f172a;
margin-top: 24px;
margin-bottom: 12px;
}}
h1 {{ font-size: 28px; }}
h2 {{ font-size: 22px; }}
h3 {{ font-size: 18px; }}
p {{ margin: 10px 0; }}
code {{
background: #f3f4f6;
padding: 2px 6px;
border-radius: 4px;
font-family: 'Courier New', monospace;
font-size: 0.9em;
}}
pre {{
background: #1e293b;
color: #e5e7eb;
padding: 12px;
border-radius: 8px;
overflow-x: auto;
}}
pre code {{
background: transparent;
padding: 0;
color: inherit;
}}
blockquote {{
border-left: 4px solid #22c55e;
padding: 8px 12px;
margin: 12px 0;
background: #f0fdf4;
border-radius: 4px;
}}
table {{
border-collapse: collapse;
width: 100%;
margin: 16px 0;
}}
th, td {{
border: 1px solid #e5e7eb;
padding: 8px 12px;
text-align: left;
}}
th {{
background: #f9fafb;
font-weight: 600;
}}
ul, ol {{
padding-left: 24px;
margin: 10px 0;
}}
li {{
margin: 6px 0;
}}
a {{
color: #2563eb;
text-decoration: none;
}}
a:hover {{
text-decoration: underline;
}}
hr {{
border: none;
border-top: 1px solid #e5e7eb;
margin: 24px 0;
}}
</style>
</head>
<body>
{html_content}
</body>
</html>"""
# Generate PDF
pdf_buffer = BytesIO()
try:
HTML(string=full_html).write_pdf(pdf_buffer)
pdf_buffer.seek(0)
return pdf_buffer
except Exception as e:
raise Exception(f"Failed to generate PDF: {str(e)}")