调整浏览器路径。
This commit is contained in:
+43
-26
@@ -1,12 +1,11 @@
|
|||||||
|
import subprocess
|
||||||
|
|
||||||
import markdown
|
import markdown
|
||||||
from playwright.async_api import async_playwright
|
from playwright.async_api import async_playwright
|
||||||
import os
|
import os
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
def playwright_browser_installed():
|
|
||||||
"""检查 Playwright 的 Chromium 浏览器是否已安装"""
|
|
||||||
chromium_path = os.path.expanduser("~/.cache/ms-playwright/chromium")
|
|
||||||
return os.path.exists(chromium_path)
|
|
||||||
# linux 下需要安装字体
|
# linux 下需要安装字体
|
||||||
# sudo apt-get install -y fonts-noto-cjk fonts-noto-cjk-extra
|
# sudo apt-get install -y fonts-noto-cjk fonts-noto-cjk-extra
|
||||||
# sudo apt-get install -y fonts-noto-color-emoji fonts-noto-cjk fonts-wqy-microhei
|
# sudo apt-get install -y fonts-noto-color-emoji fonts-noto-cjk fonts-wqy-microhei
|
||||||
@@ -161,47 +160,64 @@ def md_str_to_html(md_content, output_html):
|
|||||||
f.write('</body></html>')
|
f.write('</body></html>')
|
||||||
|
|
||||||
|
|
||||||
# 使用 Playwright 将 HTML 渲染并截图(异步版)
|
def check_chromium_installed(path):
|
||||||
|
return os.path.isfile(path) and os.access(path, os.X_OK)
|
||||||
|
|
||||||
|
|
||||||
async def html_to_image(html_file, output_image):
|
async def html_to_image(html_file, output_image):
|
||||||
"""
|
"""
|
||||||
使用 Playwright 加载 HTML 文件并截图(异步)。
|
使用 Playwright 加载 HTML 文件并截图(异步)。
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
async with async_playwright() as p:
|
async with async_playwright() as p:
|
||||||
# Windows 系统
|
browser_path = None
|
||||||
if os.name == 'nt':
|
|
||||||
|
if os.name == 'nt': # Windows
|
||||||
possible_chrome_paths = [
|
possible_chrome_paths = [
|
||||||
r"C:\Users\Liu_WIN10\AppData\Local\Google\Chrome\Application\chrome.exe",
|
r"C:\Users\Liu_WIN10\AppData\Local\Google\Chrome\Application\chrome.exe",
|
||||||
r"C:\Users\Liu-OPEN\AppData\Local\Google\Chrome\Application\chrome.exe",
|
r"C:\Users\Liu-OPEN\AppData\Local\Google\Chrome\Application\chrome.exe",
|
||||||
r"C:\Program Files\Google\Chrome\Application\chrome.exe",
|
r"C:\Program Files\Google\Chrome\Application\chrome.exe",
|
||||||
r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
|
r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
|
||||||
]
|
]
|
||||||
browser_path = next((p for p in possible_chrome_paths if os.path.exists(p)), None)
|
for path in possible_chrome_paths:
|
||||||
if browser_path:
|
if check_chromium_installed(path):
|
||||||
print(f"找到浏览器路径: {browser_path}")
|
browser_path = path
|
||||||
browser = await p.chromium.launch(executable_path=browser_path)
|
print(f"找到浏览器路径: {browser_path}")
|
||||||
else:
|
break
|
||||||
print("未找到 Chrome,尝试使用 Playwright 自带浏览器")
|
else: # Linux
|
||||||
if not playwright_browser_installed():
|
import glob
|
||||||
print("Playwright 浏览器未安装,正在安装...")
|
user_home = os.path.expanduser("~")
|
||||||
import subprocess
|
glob_pattern = os.path.join(user_home, ".cache", "ms-playwright", "chromium-*", "chrome-linux",
|
||||||
subprocess.run(["playwright", "install", "chromium"], check=True)
|
"chrome")
|
||||||
print("安装完成")
|
chrome_paths = glob.glob(glob_pattern)
|
||||||
browser = await p.chromium.launch()
|
browser_path = None
|
||||||
else:
|
for path in sorted(chrome_paths, reverse=True): # 按版本名排序,最新优先
|
||||||
if not playwright_browser_installed():
|
if check_chromium_installed(path):
|
||||||
print("Playwright 浏览器未安装,正在安装...")
|
browser_path = path
|
||||||
import subprocess
|
print(f"找到 Playwright Chromium 路径: {browser_path}")
|
||||||
subprocess.run(["playwright", "install", "chromium"], check=True)
|
break
|
||||||
print("安装完成")
|
|
||||||
browser = await p.chromium.launch()
|
|
||||||
|
|
||||||
|
if not browser_path:
|
||||||
|
print("未找到已安装的 Chromium 浏览器,尝试使用 Playwright 默认安装")
|
||||||
|
try:
|
||||||
|
print("正在安装 Playwright 浏览器...")
|
||||||
|
subprocess.run(["playwright", "install", "chromium"], check=True)
|
||||||
|
print("Playwright 浏览器安装完成")
|
||||||
|
except Exception as install_error:
|
||||||
|
print(f"安装 Playwright 浏览器失败: {install_error}")
|
||||||
|
|
||||||
|
browser = await p.chromium.launch() # 使用默认路径
|
||||||
|
else:
|
||||||
|
browser = await p.chromium.launch(executable_path=browser_path)
|
||||||
|
|
||||||
|
# 业务逻辑不变
|
||||||
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})
|
await page.set_viewport_size({"width": 750, "height": 800})
|
||||||
await page.wait_for_timeout(500)
|
await page.wait_for_timeout(500)
|
||||||
await page.screenshot(path=output_image, full_page=True)
|
await page.screenshot(path=output_image, full_page=True)
|
||||||
await browser.close()
|
await browser.close()
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"浏览器操作失败: {e}")
|
print(f"浏览器操作失败: {e}")
|
||||||
if "Executable doesn't exist" in str(e):
|
if "Executable doesn't exist" in str(e):
|
||||||
@@ -221,6 +237,7 @@ async def convert_md_str_to_image(md_content, output_image):
|
|||||||
print(f"图片已生成:{output_image}")
|
print(f"图片已生成:{output_image}")
|
||||||
return os.path.abspath(output_image)
|
return os.path.abspath(output_image)
|
||||||
|
|
||||||
|
|
||||||
# 示例使用
|
# 示例使用
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# 示例 Markdown 字符串(包含中文和 Emoji)
|
# 示例 Markdown 字符串(包含中文和 Emoji)
|
||||||
|
|||||||
Reference in New Issue
Block a user