优化md到图片的内容。
This commit is contained in:
@@ -108,9 +108,12 @@ async def html_to_image(html_content, output_image):
|
||||
context = await browser.new_context(device_scale_factor=2)
|
||||
page = await context.new_page()
|
||||
|
||||
# 设置视口宽度,高度暂设大一点以便内容铺开
|
||||
await page.set_viewport_size({"width": 750, "height": 1000})
|
||||
|
||||
# 3. 动态调整高度:先探测内容实际高度
|
||||
body_height = await page.evaluate("document.body.scrollHeight")
|
||||
await page.set_viewport_size({"width": 750, "height": body_height})
|
||||
# 2. 【关键】强制等待所有字体和 Emoji 加载完成
|
||||
# 很多时候卡住就是在等字体渲染计算
|
||||
await page.evaluate("document.fonts.ready")
|
||||
# 【优化核心】:直接设置 HTML 内容,不走 file:// 协议
|
||||
# 这样可以彻底避免文件读取超时
|
||||
await page.set_content(html_content, wait_until='load')
|
||||
@@ -119,8 +122,13 @@ async def html_to_image(html_content, output_image):
|
||||
await asyncio.sleep(0.5)
|
||||
|
||||
# 截图(full_page=True 会自动处理高度)
|
||||
await page.screenshot(path=output_image, full_page=True)
|
||||
|
||||
# 4. 截图增加超时限制,防止死锁
|
||||
await page.screenshot(
|
||||
path=output_image,
|
||||
full_page=True,
|
||||
timeout=30000, # 30秒硬超时
|
||||
animations="disabled" # 禁用可能的 CSS 动画
|
||||
)
|
||||
if not os.path.exists(output_image):
|
||||
raise RuntimeError(f"截图失败,输出文件不存在: {output_image}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user