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:
parent
9fc7217df1
commit
0d0c0025fb
2 changed files with 19 additions and 14 deletions
|
|
@ -167,13 +167,14 @@ RUN apt-get update --fix-missing && apt-get install -y --no-install-recommends \
|
||||||
|
|
||||||
# 階段 4:圖像處理工具
|
# 階段 4:圖像處理工具
|
||||||
# 注意:bookworm 使用 imagemagick(版本 6),trixie 才有 imagemagick-7
|
# 注意:bookworm 使用 imagemagick(版本 6),trixie 才有 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:文件處理工具
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue