I am using ghostscript in AWS lambda functions to generate pdf thumnail. it works for most of the pdfs but for some pdf it throws some error.

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_finishn' +
    '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 localn' +
    'Last OS error: Permission deniedn',
  stderr: 'GPL Ghostscript 9.53.3: Unrecoverable error, exit code 1n'
}

i am using gs_10.02.1_amd64_snap extracted it and using only four files to generate thumbnail.
sample file image

below is my command i used to generate thumbnail.
./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’