freelance-dashboard/md_to_pdf_final.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

212 lines
5.8 KiB
Python

#!/usr/bin/env python3
"""
Convert Markdown to PDF with perfect UTF-8 support using Playwright
"""
import markdown
import os
from playwright.sync_api import sync_playwright
def convert_md_to_pdf(input_file, output_file):
"""Convert markdown file to PDF using browser rendering"""
# 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 lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Freelance Dashboard - Technical Reference</title>
<style>
@page {{
size: A4;
margin: 2cm;
}}
body {{
font-family: Arial, Helvetica, sans-serif;
font-size: 11pt;
line-height: 1.6;
color: #333;
max-width: 800px;
margin: 0 auto;
padding: 20px;
background: white;
}}
h1 {{
color: #2C3E50;
font-size: 24pt;
text-align: center;
margin-bottom: 30px;
border-bottom: 3px solid #27AE60;
padding-bottom: 15px;
}}
h2 {{
color: #2C3E50;
font-size: 18pt;
margin-top: 25px;
margin-bottom: 15px;
border-bottom: 2px solid #ECF0F1;
padding-bottom: 8px;
page-break-before: always;
}}
h2:first-of-type {{
page-break-before: auto;
}}
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 6px;
font-family: 'Courier New', Consolas, monospace;
font-size: 9pt;
color: #C0392B;
border: 1px solid #DDDDDD;
border-radius: 3px;
}}
pre {{
background-color: #F5F5F5;
color: #2C3E50;
padding: 15px;
border: 1px solid #DDDDDD;
border-radius: 5px;
overflow-x: auto;
font-family: 'Courier New', Consolas, monospace;
font-size: 9pt;
line-height: 1.5;
page-break-inside: avoid;
}}
pre code {{
background-color: transparent;
color: #2C3E50;
padding: 0;
border: none;
}}
table {{
border-collapse: collapse;
width: 100%;
margin: 20px 0;
font-size: 10pt;
page-break-inside: avoid;
}}
th {{
background-color: #2C3E50;
color: white;
padding: 10px;
text-align: left;
border: 1px solid #ddd;
font-weight: bold;
}}
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;
background-color: #f9f9f9;
padding: 10px 15px;
}}
ul, ol {{
margin: 10px 0;
padding-left: 30px;
}}
li {{
margin: 5px 0;
}}
hr {{
border: none;
border-top: 2px solid #ECF0F1;
margin: 30px 0;
}}
</style>
</head>
<body>
{html}
</body>
</html>
"""
# Create temporary HTML file
temp_html = output_file.replace('.pdf', '_temp.html')
with open(temp_html, 'w', encoding='utf-8') as f:
f.write(html_with_style)
# Convert HTML to PDF using Playwright
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
page.goto(f'file:///{os.path.abspath(temp_html)}')
page.pdf(
path=output_file,
format='A4',
margin={
'top': '2cm',
'right': '2cm',
'bottom': '2cm',
'left': '2cm'
},
print_background=True
)
browser.close()
# Clean up temporary HTML
if os.path.exists(temp_html):
os.remove(temp_html)
print(f"PDF created successfully: {output_file}")
print(f"File size: {os.path.getsize(output_file) / 1024:.2f} KB")
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}")