Files
aletheia/docs/generate-pdfs.py

86 lines
2.4 KiB
Python

#!/usr/bin/env python3
import subprocess, base64, os, sys
from pathlib import Path
os.chdir(Path(__file__).parent)
# Load logo
logo_path = Path("../frontend/public/icons/icon-192.png")
logo_b64 = base64.b64encode(logo_path.read_bytes()).decode()
# Load template
template = Path("pdf-template.html").read_text()
from datetime import datetime
date_str = datetime.now().strftime("%d/%m/%Y")
docs = {
"MANUAL-PRODUTO": "Manual do Produto",
"MANUAL-VENDAS": "Manual de Vendas",
"MANUAL-TECNICO": "Manual Técnico",
"ARQUITETURA-TECNICA": "Arquitetura Técnica",
}
for doc, title in docs.items():
md_file = f"{doc}.md"
pdf_file = f"{doc}.pdf"
if not os.path.exists(md_file):
print(f" ⚠️ {md_file} not found, skipping")
continue
print(f"Generating {pdf_file}...")
# Convert MD to HTML
result = subprocess.run(
["pandoc", md_file, "--from", "markdown", "--to", "html"],
capture_output=True, text=True
)
body_html = result.stdout
# Build cover
cover = f'''<div class="cover">
<img src="data:image/png;base64,{logo_b64}" class="cover-logo" />
<h1>ALETHEIA</h1>
<div class="subtitle">SCANNER NUTRICIONAL COM IA</div>
<div class="doc-title">{title}</div>
<div class="version">Versão 1.0 — {date_str}</div>
<div class="tagline">"A verdade sobre o que você come"</div>
</div>
<div class="page-header">
<div style="display:flex;align-items:center;gap:10px">
<img src="data:image/png;base64,{logo_b64}" />
<span class="brand">ALETHEIA</span>
</div>
<span class="doc-type">{title}</span>
</div>'''
full_body = cover + "\n" + body_html
full_html = template.replace("$body$", full_body)
tmp_html = f"/tmp/{doc}-full.html"
Path(tmp_html).write_text(full_html, encoding="utf-8")
# Generate PDF
result = subprocess.run([
"wkhtmltopdf",
"--page-size", "A4",
"--margin-top", "20mm",
"--margin-bottom", "20mm",
"--margin-left", "20mm",
"--margin-right", "20mm",
"--enable-local-file-access",
"--print-media-type",
"--encoding", "utf-8",
"--quiet",
tmp_html, pdf_file
], capture_output=True, text=True)
if os.path.exists(pdf_file):
size = os.path.getsize(pdf_file)
print(f"{pdf_file} ({size/1024:.0f}KB)")
else:
print(f" ❌ Failed: {result.stderr[:200]}")
print("\nDone! 🎉")