#!/usr/bin/env python3
"""Renderiza um HTML -> PNG. Uso: python3 render_one.py <html> <png> <w> <h>"""
import sys
from pathlib import Path
from playwright.sync_api import sync_playwright

html_file = Path(sys.argv[1]).resolve()
png_file = Path(sys.argv[2]).resolve()
w = int(sys.argv[3])
h = int(sys.argv[4])

assert html_file.exists(), f'HTML nao existe: {html_file}'

with sync_playwright() as p:
    browser = p.chromium.launch()
    page = browser.new_page(viewport={'width': w, 'height': h}, device_scale_factor=1)
    page.goto(f'file://{html_file}')
    page.wait_for_load_state('networkidle')
    page.wait_for_timeout(500)
    page.screenshot(path=str(png_file), clip={'x': 0, 'y': 0, 'width': w, 'height': h})
    browser.close()

print(f'OK: {png_file} ({png_file.stat().st_size} bytes)')
