feat(x2pdf): 本地可用——LibreOffice 探测覆盖 mac/win 路径,WeasyPrint 缺失时回落

本地形态原先 docx/xlsx/pptx 必失败,原因不是没装 LibreOffice,而是它的可执行文件
不在 PATH 里(macOS 在 app bundle 内、Windows 在 Program Files 内,正常安装都如此),
只用 shutil.which() 会把「装了」误判成「没装」。

- find_soffice(): PATH → macOS 两个 app bundle → Windows 两个默认目录
- 文本分支:WeasyPrint 不可用(ImportError 或缺 libpango 的 OSError)时回落 LibreOffice,
  先把文档写成临时 HTML 再走同一个转换函数;返回值带一行后端提示,说明 @page/CSS 只部分生效
- 缺 markdown 包时报错点明依赖清单路径,交给模型按 requirements 自行补装
- requirements.txt:markdown 去掉版本 pin(3.x API 稳定,pin 了反而会在服务端 init
  探测失败时往 skills/deps 塞一份遮蔽系统包);weasyprint==65.1 保留,那个 pin 编码的是
  pydyf 兼容约束

线上不变:WeasyPrint 在 → 主路径照旧;仅多了一次 fallback 与更宽的 soffice 探测。
本机实测:html/txt 走回落出件正常,md 在缺 markdown 时给出可执行报错。
This commit is contained in:
2026-11-11 17:20:45 +08:00
parent 469e801319
commit 69e44c27ae
3 changed files with 71 additions and 10 deletions
+3 -1
View File
@@ -37,7 +37,9 @@ output_schema:
统一 PDF 转换入口,按扩展名自动分派两条渲染路径:
- **md / txt / html → WeasyPrint**(CSS 排版,字体走 fontconfig,中文用系统文泉驿/Noto,无需字体文件)
- **md / txt / html → WeasyPrint**(CSS 排版,字体走 fontconfig,中文用系统文泉驿/Noto,无需字体文件);
本机没有 WeasyPrint(缺包,或缺它依赖的 pango/cairo)时自动回落 LibreOffice——
能出件,但 `page`/`css` 只部分生效,不适合精确排版
- **docx / xlsx / pptx / odt / ods / odp → LibreOffice headless**(忠于原文档版式,`page`/`css` 参数不生效)
### Markdown 输入(`.md` / `.markdown`)