2025-09-24 08:35:29 +01:00
|
|
|
import pdfkit
|
2025-09-23 21:31:15 +01:00
|
|
|
from jinja2 import Environment, FileSystemLoader
|
|
|
|
|
|
2025-09-24 08:35:29 +01:00
|
|
|
env = Environment(loader=FileSystemLoader("report_gen"))
|
2025-09-23 21:31:15 +01:00
|
|
|
|
2025-09-24 08:35:29 +01:00
|
|
|
# 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"}),
|
|
|
|
|
]
|
2025-09-23 21:31:15 +01:00
|
|
|
|
2025-09-24 08:35:29 +01:00
|
|
|
# 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))
|
2025-09-23 21:31:15 +01:00
|
|
|
|
2025-09-24 08:35:29 +01:00
|
|
|
# Combine with page breaks
|
|
|
|
|
final_html = "<div class='page-break'></div>".join(html_pages)
|
2025-09-23 21:31:15 +01:00
|
|
|
|
2025-09-24 08:35:29 +01:00
|
|
|
# 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>
|
|
|
|
|
"""
|
2025-09-23 21:31:15 +01:00
|
|
|
|
2025-09-24 08:35:29 +01:00
|
|
|
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)
|
2025-09-23 21:31:15 +01:00
|
|
|
|
2025-09-24 08:35:29 +01:00
|
|
|
print("✅ PDF generated: multi_page_report.pdf")
|