"""Render an HTML file to PNG (for checking) or PDF (for the kit) with the pre-installed Chromium.""" import sys, asyncio from playwright.async_api import async_playwright async def go(src, out, width=1260, scale=1, footer=None): async with async_playwright() as p: try: b = await p.chromium.launch() except Exception: b = await p.chromium.launch(channel='chrome') # no Playwright browser installed: use Google Chrome pg = await b.new_page(viewport={'width': width, 'height': 800}, device_scale_factor=scale) await pg.goto('file://' + src); await pg.evaluate('document.fonts.ready'); await pg.wait_for_timeout(300) if out.endswith('.pdf'): kw = dict(display_header_footer=True, header_template='', footer_template= '
' + footer + ' /
') if footer else {} await pg.pdf(path=out, prefer_css_page_size=True, print_background=True, **kw) else: await pg.screenshot(path=out, full_page=True) await b.close() if __name__ == '__main__': src, out = sys.argv[1], sys.argv[2] asyncio.run(go(src, out, width=int(sys.argv[3]) if len(sys.argv) > 3 else 1260))