You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在AWS Lambda中使用Ghostscript生成PDF缩略图遇报错求助

问题:AWS Lambda中Ghostscript处理部分PDF生成缩略图失败

错误信息

ERROR   Error: Command failed: ./bin/gs -dQUIET -dPARANOIDSAFER -dBATCH -dNOPAUSE -dNOPROMPT -sDEVICE=png16m -dPDFFitPage=true -dBackgroundColor=16#FFFFFF -dUseCropBox -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r72 -dFirstPage=1 -dLastPage=1 -sOutputFile=/tmp/'e06e8302-add7-4dfe-99c8-2e54b8ffcbce_5153997.png' /tmp/'e06e8302-add7-4dfe-99c8-2e54b8ffcbce_5153997.pdf'
GPL Ghostscript 9.53.3: Unrecoverable error, exit code 1

    at ChildProcess.exithandler (node:child_process:422:12)
    at ChildProcess.emit (node:events:518:28)
    at maybeClose (node:internal/child_process:1105:16)
    at ChildProcess._handle.onexit (node:internal/child_process:305:5) {
  code: 1,
  killed: false,
  signal: null,
  cmd: "./bin/gs -dQUIET -dPARANOIDSAFER -dBATCH -dNOPAUSE -dNOPROMPT -sDEVICE=png16m -dPDFFitPage=true -dBackgroundColor=16#FFFFFF -dUseCropBox -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r72 -dFirstPage=1 -dLastPage=1 -sOutputFile=/tmp/'e06e8302-add7-4dfe-99c8-2e54b8ffcbce_5153997.png' /tmp/'e06e8302-add7-4dfe-99c8-2e54b8ffcbce_5153997.pdf'",
  stdout: '   **** Error reading a content stream. The page may be incomplete.\n' +
    '               Output may be incorrect.\n' +
    '\n' +
    "   **** Error: File has unbalanced q/Q operators (too many Q's)\n" +
    '               Output may be incorrect.\n' +
    "   **** Error: Form stream has unbalanced q/Q operators (too many q's)\n" +
    '               Output may be incorrect.\n' +
    '   **** Error reading a content stream. The page may be incomplete.\n' +
    '               Output may be incorrect.\n' +
    "   **** Error: Form stream has unbalanced q/Q operators (too many q's)\n" +
    '               Output may be incorrect.\n' +
    '   **** Error reading a content stream. The page may be incomplete.\n' +
    '               Output may be incorrect.\n' +
    "   **** Error: Form stream has unbalanced q/Q operators (too many q's)\n" +
    '               Output may be incorrect.\n' +
    'Error: /unregistered in /--pdfshowpage_finish--\n' +
    'Operand stack:\n' +
    '\n' +
    'Execution stack:\n' +
    '   %interp_exit   .runexec2   --nostringval--   pdfshowpage_finish   --nostringval--   2   %stopped_push   --nostringval--   pdfshowpage_finish   pdfshowpage_finish   false   1   %stopped_push   1974   1   3   %oparray_pop   1973   1   3   %oparray_pop   1961   1   3   %oparray_pop   1962   1   3   %oparray_pop   pdfshowpage_finish   pdfshowpage_finish   2   1   1   pdfshowpage_finish   %for_pos_int_continue   1965   1   7   %oparray_pop   pdfshowpage_finish   pdfshowpage_finish\n' +
    'Dictionary stack:\n' +
    '   --dict:736/1123(ro)(G)--   --dict:1/20(G)--   --dict:80/200(L)--   --dict:80/200(L)--   --dict:133/256(ro)(G)--   --dict:320/325(ro)(G)--   --dict:31/32(L)--   --dict:6/9(L)--   --dict:6/20(L)--\n' +
    'Current allocation mode is local\n' +
    'Last OS error: Permission denied\n',
  stderr: 'GPL Ghostscript 9.53.3: Unrecoverable error, exit code 1\n'
}

环境与执行命令

使用提取后的gs_10.02.1_amd64_snap(仅保留4个文件),执行命令:

./bin/gs -dQUIET -dPARANOIDSAFER -dBATCH -dNOPAUSE -dNOPROMPT -sDEVICE=png16m -dPDFFitPage=true -dBackgroundColor=16#FFFFFF -dUseCropBox -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r72 -dFirstPage=1 -dLastPage=1 -sOutputFile=${directory}'${newUUID + "_" + randomNumber}.png' ${directory}'${newUUID + "_" + randomNumber}.pdf'

排查与解决步骤

1. 修复PDF文件本身的语法错误

报错显示PDF存在q/Q操作符不平衡(PDF内容流中的图形状态压栈/出栈操作不匹配),这类语法错误会导致Ghostscript处理中断。可以先尝试用Ghostscript修复PDF:

gs -o /tmp/fixed_${newUUID}_${randomNumber}.pdf -sDEVICE=pdfwrite /tmp/${newUUID}_${randomNumber}.pdf

再用修复后的PDF生成缩略图。

2. 调整Ghostscript命令参数

  • 替换-dPARANOIDSAFER为-dSAFER:前者会严格限制文件系统访问,可能触发Lambda环境下的权限问题,后者保持安全但限制更宽松。
  • 添加错误忽略参数:加入-dIgnorePDFErrors或-dNODEBUG,让Ghostscript跳过PDF语法错误继续生成输出。
  • 修正路径引号:命令中路径的单引号可能导致解析异常,直接使用无引号的路径,例如:
    -sOutputFile=/tmp/${newUUID}_${randomNumber}.png /tmp/${newUUID}_${randomNumber}.pdf
    

3. 确认Ghostscript版本一致性

报错显示运行的是9.53.3,但你部署的是10.02.1,说明Lambda环境中实际调用的是旧版本。检查:

  • 部署包/层中的Ghostscript文件路径是否正确,确保./bin/gs指向10.02.1版本的二进制文件。
  • 给二进制文件添加可执行权限:在部署前执行chmod +x ./bin/gs,避免因权限不足无法运行。

4. 排查Lambda环境权限

Lambda的/tmp目录默认可读写,但需确保Ghostscript的所有依赖文件(如库文件)都包含在部署包中,且权限设置正确,避免因缺少依赖或权限问题导致执行失败。


内容的提问来源于stack exchange,提问作者Lakshmanan M

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 11:10:59