动态图片高度,

This commit is contained in:
liuwei
2025-05-29 10:56:38 +08:00
parent 762a6c52e1
commit ddf1d0a233

View File

@@ -214,9 +214,13 @@ async def html_to_image(html_file, output_image):
try:
page = await browser.new_page()
await page.goto(f'file://{os.path.abspath(html_file)}')
await page.set_viewport_size({"width": 750, "height": 800})
await page.wait_for_timeout(500)
await page.screenshot(path=output_image, full_page=True)
# 获取页面内容的实际高度
content_height = await page.evaluate("document.body.scrollHeight")
# 设置视口大小,宽度固定为 750高度根据内容动态调整加 50px 缓冲)
await page.set_viewport_size({"width": 750, "height": content_height + 50})
await page.wait_for_timeout(500) # 等待渲染
await page.screenshot(path=output_image, full_page=False) # full_page=False 以使用视口大小
print(f"图片生成成功,动态高度: {content_height + 50}px")
except Exception as e:
print(f"截图失败: {e}")
finally: