Files
bio-performx/main.py
T

55 lines
1.3 KiB
Python
Raw Normal View History

import pdfkit
from jinja2 import Environment, FileSystemLoader
env = Environment(loader=FileSystemLoader("report_gen"))
# Define templates and their unique contexts
pages = [
("page_1.html", {"name": "John Doe", "surname": "Moran", "date": "July 29, 2025"}),
("page_2.html", {"content": "This is page 2 content"}),
]
# Render each template with its own context
html_pages = []
for tpl, ctx in pages:
template = env.get_template(tpl)
html_pages.append(template.render(ctx))
# Combine with page breaks
final_html = "<div class='page-break'></div>".join(html_pages)
# Wrap in full HTML document
html_doc = f"""
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link href="https://cdn.jsdelivr.net/npm/tailwindcss/dist/tailwind.min.css" rel="stylesheet">
<style>
.page-break {{ page-break-after: always; }}
.page {{
height: 386mm;
}}
</style>
</head>
<body class="m-0 p-0">
{final_html}
</body>
</html>
"""
print(html_doc)
# Generate PDF
options = {
"page-size": "A4",
"encoding": "UTF-8",
"margin-top": "0mm",
"margin-bottom": "0mm",
"margin-left": "0mm",
"margin-right": "0mm",
"no-outline": None,
}
pdfkit.from_string(html_doc, "multi_page_report.pdf", options=options)
print("✅ PDF generated: multi_page_report.pdf")