from playwright.sync_api import sync_playwright def html_to_pdf(html_path, pdf_path): with sync_playwright() as p: browser = p.chromium.launch() page = browser.new_page() # Load local HTML file page.goto(f"file://{html_path}") # Export to PDF with A4 size page.pdf( path=pdf_path, format="A4", # <-- built-in A4 support margin={"top": "20mm", "bottom": "20mm", "left": "15mm", "right": "15mm"}, print_background=True # include Tailwind background colors/images ) browser.close() # Example usage html_to_pdf("table_of_contents.html", "report.pdf")