phufbv
phufbv

Reputation: 63

Ghostscript setup - error converting any jpg to pdf

I'm trying to use Ghostscript v9.52 on a Windows 10 machine, but I get a blank pdf and this error when I try to convert any jpg file to pdf:

Error: /undefined in Test-JPG.jpg
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--
Dictionary stack:
   --dict:738/1123(ro)(G)--   --dict:0/20(G)--   --dict:78/200(L)--
Current allocation mode is local
GPL Ghostscript 9.52: Unrecoverable error, exit code 1

I've tried the following commands:

C:\Users\Work\Desktop>gswin64c -sDEVICE=pdfwrite -o myFile.pdf "C:\Program Files\gs\gs9.52\lib\viewjpeg.ps" -c Test-JPG.jpg viewJPEG
C:\Users\Work\Desktop>gswin64c -sDEVICE=pdfwrite -o out.pdf viewjpeg.ps -c "Test-JPG.jpg viewJPEG" -f 
C:\Users\Work\Desktop>gswin64c -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=output.pdf Test-JPG.jpg

What I've tried so far:

The closest answer I've found otherwise is this question, however the answer there is the second command above, which doesn't work for me. Given it's happening with all jpgs I try, I wonder if there's something I've not set up correctly when installing ghostscript?

Upvotes: 1

Views: 1857

Answers (1)

KenS
KenS

Reputation: 31141

Ghostscript is a PostScript interpreter, it also incorporates a PostScript program which will interpree PDF files. That's all. It does not (currently) directly read any image formats.

However, PostScript is a programming language and there are helper PostScript programs supplied with Ghostscript which will read some image formats.

Your first command line runs viewjpeg,ps then executes the -c switch, which causes the command line to be read as a PostScript program until the next -f. "Test-JPG.jpg viewJPEG" isn't valid PostScript (there is no operator or function called Test-JPG.jpg" in the PostScript language or the ViewJPEG.ps program), so it doesn't work.

The second command line is essentially the same, just wrapped in "" so it also doesn't work. It also doesn't supply a fully qualified path to viewjpeg.ps so it probably fails unable to find viewjpg.ps.

Your third command line doesn't even use viewjpeg.ps so it tries to read the JPEG contents as PostScript which, unsurprisingly, also doesn't work.

If you read viewjpeg.ps it actually has an example usage in the comments:

% Usage example:
%   (jpeg-6/testimg.jpg) viewJPEG

Noice the () surrounding the filename, those are missing from your command lines. If you do this:

gswin64c -sDEVICE=pdfwrite -o myFile.pdf "C:\Program Files\gs\gs9.52\lib\viewjpeg.ps" -c "(Test-JPG.jpg) viewJPEG" -f

Then it will probably work. Assuming all the files are in the right place.

However, since version 9.50 the default behaviour of Ghostscript is SAFER, rather than NOSAFER. This means that PostScript programs (like viewJPEG.ps) are not permitted to access arbitrary files from disk.

Ghostscript goes to some lengths to try and make sure that any files specified on the command line as input or output files are permitted, but if you want a PostScript program to read a file you either have to run with -dNOSAFER whcih is safe in this case but I would not reccomend doing it, or you have to add the file to the permitted file read list.

This is explained in the documentation here scroll down to SAFER to see it.

Upvotes: 3

Related Questions