fix: Inkscape GTK 錯誤 - 使用 xvfb-run 包裝

- 安裝 xvfb 到 Docker image
- 使用 xvfb-run -a 包裝 inkscape 命令
- 設定虛擬螢幕解析度 1024x768x24
- 修復 PNG 轉 SVG 等需要 GTK 的操作

Fixes: Gtk-ERROR Can't create GtkStyleContext without display
This commit is contained in:
Your Name 2026-01-23 11:57:26 +08:00
parent 9fc7217df1
commit 0d0c0025fb
2 changed files with 19 additions and 14 deletions

View file

@ -167,13 +167,14 @@ RUN apt-get update --fix-missing && apt-get install -y --no-install-recommends \
# 階段 4圖像處理工具 # 階段 4圖像處理工具
# 注意bookworm 使用 imagemagick版本 6trixie 才有 imagemagick-7 # 注意bookworm 使用 imagemagick版本 6trixie 才有 imagemagick-7
# 注意Inkscape 1.0+ 使用 --export-type/--export-filename 語法,支援 headless 執行,不需要 xvfb # 注意Inkscape 需要 xvfb 在無 DISPLAY 環境下執行某些操作(如 PNG 轉 SVG
RUN apt-get update --fix-missing && apt-get install -y --no-install-recommends \ RUN apt-get update --fix-missing && apt-get install -y --no-install-recommends \
imagemagick \ imagemagick \
inkscape \ inkscape \
libheif-examples \ libheif-examples \
libjxl-tools \ libjxl-tools \
libvips-tools \ libvips-tools \
xvfb \
&& rm -rf /var/lib/apt/lists/* && rm -rf /var/lib/apt/lists/*
# 階段 5文件處理工具 # 階段 5文件處理工具

View file

@ -33,14 +33,11 @@ export const properties = {
* Inkscape * Inkscape
* *
* Headless * Headless
* Inkscape 1.0+ headless * Inkscape PNG SVG GTK
* X11 xvfb * DISPLAY 使 xvfb-run
* *
* 🔧 headless-safe * 🔧 使 xvfb-run headless
* inkscape input.png --export-type=svg --export-filename=output.svg * xvfb-run -a inkscape input.png --export-type=svg --export-filename=output.svg
*
* GTK
* inkscape input.png -o output.svg
* *
* https://inkscape.org/doc/inkscape-man.html * https://inkscape.org/doc/inkscape-man.html
*/ */
@ -56,13 +53,20 @@ export function convert(
// 從目標路徑取得輸出格式(移除開頭的點) // 從目標路徑取得輸出格式(移除開頭的點)
const exportType = extname(targetPath).slice(1).toLowerCase(); const exportType = extname(targetPath).slice(1).toLowerCase();
// 使用 Inkscape 1.0+ 的 headless-safe 命令列語法 // 使用 xvfb-run 包裝 Inkscape 命令,確保在無 DISPLAY 環境下也能運作
// --export-type: 明確指定輸出格式 // -a: 自動選擇可用的 display number
// --export-filename: 指定輸出檔案路徑 // --server-args="-screen 0 1024x768x24": 設定虛擬螢幕解析度
// 這種語法不會初始化 GTK因此在無 DISPLAY 的環境也能運作 const args = [
const args = [filePath, `--export-type=${exportType}`, `--export-filename=${targetPath}`]; "-a",
"--server-args=-screen 0 1024x768x24",
"inkscape",
filePath,
`--export-type=${exportType}`,
`--export-filename=${targetPath}`,
];
execFile("inkscape", args, (error: Error | null, stdout: string, stderr: string) => { // 使用 xvfb-run 而非直接呼叫 inkscape
execFile("xvfb-run", args, (error: Error | null, stdout: string, stderr: string) => {
if (error) { if (error) {
reject(`error: ${error}`); reject(`error: ${error}`);
return; return;