Search code examples
pdfjpegghostscript

ghostscript stackunderflow in --dup-- error


I was trying to convert jpg to pdf using ghost script. The command which I was using was

 C:\Users\kbged\Desktop\XAMPP\php\bin\gs\gs9.54.0\bin\gswin64c  -sDEVICE=pdfwrite -sOutputFile=C:\Users\kbged\Pictures\php48D4.pdf C:\Users\kbged\Desktop\XAMPP\php\bin\gs\gs9.54.0\lib\viewjpeg.ps -c (C:\Users\kbged\Pictures\group_image.jpg) viewJPEG

However, this end with following error:

GPL Ghostscript 9.54.0 (2021-03-30)
Copyright (C) 2021 Artifex Software, Inc.  All rights reserved.
This software is supplied under the GNU AGPLv3 and comes with NO WARRANTY:
see the file COPYING for details.
Error: /stackunderflow in --dup--
Operand stack:

Execution stack:
   %interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--   --nostringval--   false   1   %stopped_push   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--
Dictionary stack:
   --dict:732/1123(ro)(G)--   --dict:0/20(G)--   --dict:78/200(L)--   --dict:8/20(L)--
Current allocation mode is local
GPL Ghostscript 9.54.0: Unrecoverable error, exit code 1

I tried to look into that viewjpeg.ps file and almost towards the end I see


% This lets you do stuff on the command line like:
% gs -sDEVICE=pdfwrite -o stuff%03d.pdf viewjpeg.ps -c "(image.jpg) << /PageSize 2 index viewJPEGgetsize 2 array astore  >> setpagedevice viewJPEG"
% so the output size matches the original image.

I actually don't understand where the error lies... Any help would be appreciated! Thanks!


Solution

  • There are several ways to convert image to PDF and this is possibly not the best way to convert Jpeg to PDF in Ghostscript, but you asked why it did not work for you.

    The following should in a cmd file, first work per the example, once proven then try changing the <> of the last line that you think need altering. Note that long lines are very fragile and there must be NO space after ^

    Also the order and combinations can easily throw GS into an error state. CMD Filenames are best always "FullPath\quoted" and if you don't want to inititally CD to the output directory (to make it much easier to avoid using escaped name) then change the start of last line to

    -c "("C:\\Users\\kbged\\Pictures\\group_image.jpg") <<......

    Beware the much simpler method would be to write your own input.PS to avoid potential command line truncation.

    @echo off
    REM switch to folder of jpeg
    cd "C:\Users\kbged\Pictures"
    "C:\Users\kbged\Desktop\XAMPP\php\bin\gs\gs9.54.0\bin\gswin64c.exe" -dNOSAFER -sDEVICE=pdfwrite ^
    -o "C:\Users\kbged\Pictures\php48D4.pdf" "C:\Users\kbged\Desktop\XAMPP\php\bin\gs\gs9.54.0\lib\viewjpeg.ps" ^
    -c "(group_image.jpg) << /PageSize 2 index viewJPEGgetsize 2 array astore >> setpagedevice viewJPEG"