From 43c334354f580dfa34433595d759c8fb12a245c9 Mon Sep 17 00:00:00 2001 From: liuwei Date: Fri, 17 Apr 2026 09:18:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=BC=BA=E8=BD=AC=E5=9B=BE=E6=B5=8F?= =?UTF-8?q?=E8=A7=88=E5=99=A8=E5=90=AF=E5=8A=A8=E5=81=A5=E5=A3=AE=E6=80=A7?= =?UTF-8?q?=E5=B9=B6=E4=BF=AE=E6=AD=A3=E6=97=A5=E5=BF=97=E6=9D=A5=E6=BA=90?= =?UTF-8?q?=E6=A0=87=E8=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 变更项:1) html_to_image 改为候选浏览器逐个尝试启动,避免单一路径失败导致整体异常。2) Linux 增加系统浏览器路径探测(google-chrome/chromium)并保留 Playwright 缓存路径作为候选。3) 修正启动日志来源标识,区分 system 与 playwright-cache,避免误判。4) 所有候选失败时自动回退到 bundled 浏览器,提高转图成功率。 --- utils/markdown_to_image.py | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/utils/markdown_to_image.py b/utils/markdown_to_image.py index d660d30..7d1d399 100644 --- a/utils/markdown_to_image.py +++ b/utils/markdown_to_image.py @@ -1,6 +1,7 @@ import subprocess import time from pathlib import Path +import shutil import psutil from playwright.async_api import async_playwright @@ -478,8 +479,9 @@ def check_chromium_installed(path): async def html_to_image(html_content, output_image): async with async_playwright() as p: - browser_path = None + browser_candidates = [] if os.name == 'nt': + # Windows 优先尝试常见系统安装路径。 possible_chrome_paths = [ r"C:\Users\Liu_WIN10\AppData\Local\Google\Chrome\Application\chrome.exe", r"C:\Users\Liu-OPEN\AppData\Local\Google\Chrome\Application\chrome.exe", @@ -488,24 +490,41 @@ async def html_to_image(html_content, output_image): ] for path in possible_chrome_paths: if check_chromium_installed(path): - browser_path = path - break + browser_candidates.append(("system", path)) else: import glob + # Linux 先尝试系统可执行文件,再尝试 Playwright 缓存浏览器。 + for bin_name in ("google-chrome", "google-chrome-stable", "chromium", "chromium-browser"): + bin_path = shutil.which(bin_name) + if bin_path and check_chromium_installed(bin_path): + browser_candidates.append(("system", bin_path)) user_home = os.path.expanduser("~") glob_pattern = os.path.join(user_home, ".cache", "ms-playwright", "chromium-*", "chrome-linux", "chrome") chrome_paths = glob.glob(glob_pattern) for path in sorted(chrome_paths, reverse=True): if check_chromium_installed(path): - browser_path = path - break + browser_candidates.append(("playwright-cache", path)) launch_args = ["--no-sandbox", "--disable-setuid-sandbox", "--disable-dev-shm-usage", "--disable-gpu"] + browser = None + launch_errors = [] - if browser_path: - logger.debug(f"Launch chromium with system chrome: {browser_path}") - browser = await p.chromium.launch(executable_path=browser_path, args=launch_args) - else: + # 优先按候选路径逐个尝试,失败自动降级,不让单一路径问题导致整体失败。 + for source, browser_path in browser_candidates: + try: + logger.debug(f"Launch chromium with {source}: {browser_path}") + browser = await p.chromium.launch( + executable_path=browser_path, + args=launch_args, + timeout=20000, + ) + break + except Exception as e: + launch_errors.append(f"{source}:{browser_path} -> {e}") + logger.warning(f"Launch chromium failed with {source}: {browser_path}, error={e}") + + # 如果候选都失败,回退到 Playwright bundled 浏览器。 + if not browser: logger.debug("Launch chromium with bundled browser") browser = await p.chromium.launch(args=launch_args)