-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkdown_to_pdf.py
141 lines (115 loc) · 3.33 KB
/
markdown_to_pdf.py
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
# tender_report.py
from markdown import Markdown
from weasyprint import HTML, CSS
from weasyprint.text.fonts import FontConfiguration
import os
from datetime import datetime
def format_tender_report(tenders, client_name):
"""
Format tender data into markdown text
"""
markdown_text = f"""# Tender Report for {client_name}
Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
## Available Tenders
"""
for tender in tenders:
markdown_text += f"""### {tender['title']}
- **Tender ID**: {tender.get('tender_id', 'N/A')}
- **Bid Deadline**: {tender.get('deadline', 'N/A')}
- **Estimated Value**: {tender.get('value', 'N/A')}
**Description**:
{tender.get('description', 'No description available')}
---
"""
return markdown_text
def create_tender_pdf(markdown_text, output_path):
"""
Convert tender report markdown to PDF
"""
try:
md = Markdown(extensions=[
'tables',
'fenced_code',
'codehilite',
'toc',
'smarty'
])
html = md.convert(markdown_text)
css = '''
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap');
body {
font-family: 'Inter', sans-serif;
line-height: 1.4;
max-width: 800px;
margin: 0 auto;
padding: 1em;
color: #333;
}
h1 {
color: #1a365d;
font-size: 1.8em;
border-bottom: 2px solid #e2e8f0;
padding-bottom: 0.3em;
margin-bottom: 1em;
}
h2 {
color: #2d3748;
font-size: 1.5em;
margin-top: 1.5em;
margin-bottom: 0.5em;
}
h3 {
color: #4a5568;
font-size: 1.2em;
margin-top: 1em;
margin-bottom: 0.5em;
}
p {
margin: 0.5em 0;
}
ul {
margin: 0.5em 0;
padding-left: 1.5em;
}
li {
margin: 0.2em 0;
}
strong {
color: #2d3748;
}
hr {
border: none;
border-top: 1px solid #e2e8f0;
margin: 1em 0;
}
@page {
margin: 2cm;
@bottom-right {
content: counter(page);
font-size: 0.9em;
color: #718096;
}
}
'''
full_html = f'''
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Tender Report</title>
</head>
<body>
{html}
</body>
</html>
'''
font_config = FontConfiguration()
HTML(string=full_html).write_pdf(
output_path,
stylesheets=[CSS(string=css, font_config=font_config)],
font_config=font_config
)
return output_path
except Exception as e:
print(f"Error creating PDF: {e}")
raise