Update main.py
Browse files
main.py
CHANGED
|
@@ -290,7 +290,28 @@ async def convert_html_to_image(request: ImageRequest):
|
|
| 290 |
temp_image_path = f"temp/{unique_id}.png"
|
| 291 |
|
| 292 |
# 使用DrissionPage进行截图
|
| 293 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 294 |
|
| 295 |
try:
|
| 296 |
# 设置页面视口大小
|
|
@@ -326,7 +347,8 @@ async def convert_html_to_image(request: ImageRequest):
|
|
| 326 |
page.screenshot(path=temp_image_path)
|
| 327 |
|
| 328 |
finally:
|
| 329 |
-
page
|
|
|
|
| 330 |
|
| 331 |
return ImageResponse(
|
| 332 |
success=True,
|
|
|
|
| 290 |
temp_image_path = f"temp/{unique_id}.png"
|
| 291 |
|
| 292 |
# 使用DrissionPage进行截图
|
| 293 |
+
from drivisionpage import ChromiumPage
|
| 294 |
+
import os
|
| 295 |
+
import time
|
| 296 |
+
|
| 297 |
+
# 简单的重试逻辑避免浏览器启动问题
|
| 298 |
+
max_retries = 3
|
| 299 |
+
retry_count = 0
|
| 300 |
+
page = None
|
| 301 |
+
|
| 302 |
+
while retry_count < max_retries:
|
| 303 |
+
try:
|
| 304 |
+
# 创建页面实例(使用简单的参数避免环境兼容性问题)
|
| 305 |
+
page = ChromiumPage()
|
| 306 |
+
break
|
| 307 |
+
except Exception as e:
|
| 308 |
+
retry_count += 1
|
| 309 |
+
if retry_count >= max_retries:
|
| 310 |
+
raise e
|
| 311 |
+
time.sleep(0.5) # 等待后重试
|
| 312 |
+
|
| 313 |
+
if page is None:
|
| 314 |
+
raise HTTPException(status_code=500, detail="浏览器启动失败,请检查环境配置")
|
| 315 |
|
| 316 |
try:
|
| 317 |
# 设置页面视口大小
|
|
|
|
| 347 |
page.screenshot(path=temp_image_path)
|
| 348 |
|
| 349 |
finally:
|
| 350 |
+
if page:
|
| 351 |
+
page.quit()
|
| 352 |
|
| 353 |
return ImageResponse(
|
| 354 |
success=True,
|