45 lines
1.3 KiB
Python
45 lines
1.3 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
Генерация PDF из Markdown через weasyprint.
|
||
|
||
УСТАНОВКА: pip install weasyprint
|
||
ИСПОЛЬЗОВАНИЕ: python3 generate_pdf.py report.md report.pdf
|
||
"""
|
||
|
||
import sys
|
||
from weasyprint import HTML
|
||
|
||
def markdown_to_pdf(md_path, pdf_path):
|
||
"""Конвертирует Markdown в PDF через HTML."""
|
||
with open(md_path, 'r', encoding='utf-8') as f:
|
||
md_content = f.read()
|
||
|
||
# Simple MD to HTML conversion
|
||
html_content = f"""
|
||
<html lang="ru">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<style>
|
||
@page {{ margin: 2cm; }}
|
||
body {{ font-family: sans-serif; line-height: 1.6; }}
|
||
h1 {{ color: #2c3e50; border-bottom: 2px solid #3498db; }}
|
||
h2 {{ color: #34495e; border-bottom: 1px solid #bdc3c7; }}
|
||
table {{ border-collapse: collapse; width: 100%; }}
|
||
th, td {{ border: 1px solid #ddd; padding: 8px; }}
|
||
</style>
|
||
</head>
|
||
<body>
|
||
{md_content.replace('## ', '<h2>').replace('# ', '<h1>')}
|
||
</body>
|
||
</html>
|
||
"""
|
||
|
||
HTML(string=html_content).write_pdf(pdf_path)
|
||
|
||
if __name__ == "__main__":
|
||
if len(sys.argv) < 3:
|
||
print("Usage: python3 generate_pdf.py <report.md> <output.pdf>")
|
||
sys.exit(1)
|
||
|
||
markdown_to_pdf(sys.argv[1], sys.argv[2])
|
||
print(f"PDF created: {sys.argv[2]}")
|