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 = "
".join(html_pages) # Wrap in full HTML document html_doc = f""" {final_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")