动态图片高度,
This commit is contained in:
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user