#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""整页截图: 复盘报告 HTML → 全页长图 + 首屏图"""
import asyncio
from playwright.async_api import async_playwright

URL = "http://127.0.0.1/report_20260813.html"
FULL = "/root/web_reports/preview_full_20260813.jpg"
TOP  = "/root/web_reports/preview_top_20260813.png"

async def main():
    async with async_playwright() as p:
        browser = await p.chromium.launch(args=["--font-render-hinting=none"])
        page = await browser.new_page(viewport={"width": 1440, "height": 900},
                                      device_scale_factor=1.5)
        await page.goto(URL, wait_until="networkidle")
        await page.wait_for_timeout(800)
        # 首屏
        await page.screenshot(path=TOP, clip={"x": 0, "y": 0, "width": 1440, "height": 900})
        # 全页长图 (jpeg 压缩, 邮件友好)
        await page.screenshot(path=FULL, full_page=True, type="jpeg", quality=82)
        total = await page.evaluate("document.body.scrollHeight")
        print(f"OK full_page_height={total}px -> {FULL}")
        await browser.close()

asyncio.run(main())