-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreporting.py
More file actions
31 lines (23 loc) · 873 Bytes
/
reporting.py
File metadata and controls
31 lines (23 loc) · 873 Bytes
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
from __future__ import annotations
from io import BytesIO
from pathlib import Path
from typing import Dict, Any
from jinja2 import Environment, FileSystemLoader, select_autoescape
from xhtml2pdf import pisa
def render_html_report(context: Dict[str, Any]) -> str:
templates_dir = Path(__file__).parent / "templates"
env = Environment(
loader=FileSystemLoader(str(templates_dir)),
autoescape=select_autoescape(["html", "xml"]),
)
template = env.get_template("report.html")
html = template.render(**context)
return html
def generate_pdf_from_html(html: str) -> bytes:
buffer = BytesIO()
# xhtml2pdf expects UTF-8
result = pisa.CreatePDF(src=html, dest=buffer, encoding="UTF-8")
if result.err:
# Return whatever was generated to aid debugging
return buffer.getvalue()
return buffer.getvalue()