freelance-dashboard/md_to_pdf_simple.py
StillHammer 21ebf32e4f Dashboard avec graphiques, TCD, slicers et checkboxes fonctionnels
- KPIs complets (CA, Heures, Taux, Stats, Objectifs)
- Graphiques CA par client + Heures par client
- TCD Heures x Client x Projet avec Slicer interactif
- Checkboxes dynamiques bien positionnes (E3:E7)
- Documentation technique et guides MCP

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-21 17:05:25 +07:00

164 lines
4.6 KiB
Python

#!/usr/bin/env python3
"""
Simple Markdown to PDF converter using markdown and xhtml2pdf
"""
import markdown
from xhtml2pdf import pisa
import os
def convert_md_to_pdf(input_file, output_file):
"""Convert markdown file to PDF"""
# Read markdown file
with open(input_file, 'r', encoding='utf-8') as f:
md_content = f.read()
# Convert markdown to HTML
html = markdown.markdown(md_content, extensions=['tables', 'fenced_code', 'codehilite'])
# Add CSS styling
html_with_style = f"""
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
@page {{
size: A4;
margin: 2cm;
}}
body {{
font-family: 'Arial', sans-serif;
font-size: 11pt;
line-height: 1.6;
color: #333;
}}
h1 {{
color: #2C3E50;
font-size: 24pt;
text-align: center;
margin-bottom: 30px;
border-bottom: 3px solid #27AE60;
padding-bottom: 10px;
}}
h2 {{
color: #2C3E50;
font-size: 18pt;
margin-top: 25px;
margin-bottom: 15px;
border-bottom: 2px solid #ECF0F1;
padding-bottom: 5px;
}}
h3 {{
color: #27AE60;
font-size: 14pt;
margin-top: 20px;
margin-bottom: 10px;
}}
h4 {{
color: #2C3E50;
font-size: 12pt;
margin-top: 15px;
margin-bottom: 8px;
}}
p {{
margin: 10px 0;
text-align: justify;
}}
code {{
background-color: #F5F5F5;
padding: 2px 5px;
font-family: 'Courier New', monospace;
font-size: 9pt;
color: #C0392B;
border: 1px solid #DDDDDD;
}}
pre {{
background-color: #F5F5F5;
color: #2C3E50;
padding: 15px;
border: 1px solid #DDDDDD;
overflow-x: auto;
font-family: 'Courier New', monospace;
font-size: 9pt;
line-height: 1.5;
}}
pre code {{
background-color: transparent;
color: #2C3E50;
padding: 0;
}}
table {{
border-collapse: collapse;
width: 100%;
margin: 20px 0;
font-size: 10pt;
}}
th {{
background-color: #2C3E50;
color: white;
padding: 10px;
text-align: left;
border: 1px solid #ddd;
}}
td {{
padding: 8px;
border: 1px solid #ddd;
}}
tr:nth-child(even) {{
background-color: #f9f9f9;
}}
blockquote {{
border-left: 4px solid #27AE60;
padding-left: 15px;
margin: 20px 0;
font-style: italic;
color: #555;
}}
ul, ol {{
margin: 10px 0;
padding-left: 30px;
}}
li {{
margin: 5px 0;
}}
hr {{
border: none;
border-top: 2px solid #ECF0F1;
margin: 30px 0;
}}
.page-break {{
page-break-after: always;
}}
</style>
</head>
<body>
{html}
</body>
</html>
"""
# Convert HTML to PDF with encoding
with open(output_file, 'wb') as pdf_file:
pisa_status = pisa.CreatePDF(
html_with_style.encode('utf-8'),
dest=pdf_file,
encoding='utf-8'
)
if pisa_status.err:
print(f"Error creating PDF: {pisa_status.err}")
return False
else:
print(f"PDF created successfully: {output_file}")
return True
if __name__ == '__main__':
input_file = r'C:\Users\alexi\Documents\projects\freelance-dashboard\TECHNICAL_REFERENCE_EN.md'
output_file = r'C:\Users\alexi\Documents\projects\freelance-dashboard\TECHNICAL_REFERENCE_EN.pdf'
if os.path.exists(input_file):
convert_md_to_pdf(input_file, output_file)
else:
print(f"Input file not found: {input_file}")