动态图片高度,

This commit is contained in:
liuwei
2025-05-29 10:59:10 +08:00
parent ddf1d0a233
commit 77dbe54f73

View File

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