fix(inkscape): use headless-safe export syntax (--export-type/--export-filename)

Breaking change from Inkscape 1.0+:
- Old syntax: inkscape input.png -o output.svg (triggers GTK init)
- New syntax: inkscape input.png --export-type=svg --export-filename=output.svg

This is the correct headless-safe approach, no need for xvfb.

Ref: https://inkscape.org/doc/inkscape-man.html
This commit is contained in:
Your Name 2026-01-23 00:01:24 +08:00
parent b5992a84e0
commit 26304a295e
3 changed files with 52 additions and 80 deletions

View file

@ -1,4 +1,5 @@
import { execFile as execFileOriginal } from "node:child_process";
import { extname } from "node:path";
import { ExecFileFn } from "./types";
export const properties = {
@ -32,12 +33,16 @@ export const properties = {
* Inkscape
*
* Headless
* Inkscape X11 Docker/Server
* 使 xvfb-run
* Inkscape 1.0+ headless
* X11 xvfb
*
* 🔧
* 1. 使 xvfb-run X11
* 2. xvfb-run GDK_BACKEND=svg SVG
* 🔧 headless-safe
* inkscape input.png --export-type=svg --export-filename=output.svg
*
* GTK
* inkscape input.png -o output.svg
*
* https://inkscape.org/doc/inkscape-man.html
*/
export function convert(
filePath: string,
@ -48,45 +53,18 @@ export function convert(
execFile: ExecFileFn = execFileOriginal, // to make it mockable
): Promise<string> {
return new Promise((resolve, reject) => {
// 使用 xvfb-run 執行 Inkscape建立虛擬 X11 顯示器)
// -a: 自動尋找可用的 display number
// --server-args: 設定虛擬螢幕解析度
const xvfbArgs = [
"-a",
"--server-args=-screen 0 1024x768x24",
"inkscape",
filePath,
"-o",
targetPath,
];
// 從目標路徑取得輸出格式(移除開頭的點)
const exportType = extname(targetPath).slice(1).toLowerCase();
execFile("xvfb-run", xvfbArgs, (error: Error | null, stdout: string, stderr: string) => {
// 使用 Inkscape 1.0+ 的 headless-safe 命令列語法
// --export-type: 明確指定輸出格式
// --export-filename: 指定輸出檔案路徑
// 這種語法不會初始化 GTK因此在無 DISPLAY 的環境也能運作
const args = [filePath, `--export-type=${exportType}`, `--export-filename=${targetPath}`];
execFile("inkscape", args, (error: Error | null, stdout: string, stderr: string) => {
if (error) {
// xvfb-run 失敗,回退到直接執行 inkscape
// 這可能在某些已有 DISPLAY 設定的環境中運作
console.log("[Inkscape] xvfb-run failed, trying direct inkscape execution...");
console.log(`[Inkscape] Original error: ${error.message}`);
execFile(
"inkscape",
[filePath, "-o", targetPath],
(error2: Error | null, stdout2: string, stderr2: string) => {
if (error2) {
reject(`error: ${error2}`);
return;
}
if (stdout2) {
console.log(`stdout: ${stdout2}`);
}
if (stderr2) {
console.error(`stderr: ${stderr2}`);
}
resolve("Done");
},
);
reject(`error: ${error}`);
return;
}