问题
I'm trying to compress pdf files using ghostscript like this:
gs -sDEVICE=pdfwrite -dPDFSETTINGS=/ebook -dCompatibilityLevel=1.4 -dNOPAUSE -dBATCH -sOutputfile=output.pdf input.pdf
I've done this successfully in the past, but for some reason now it won't work. I get the following error:
GPL Ghostscript 9.15 (2014-09-22)
Copyright (C) 2014 Artifex Software, Inc. All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
**** Unable to open the initial device, quitting.
Unrecoverable error: undefinedfilename in setpagedevice
Operand stack:
true --nostringval-- --nostringval-- --nostringval-- --nostringval-- --nostringval-- --nostringval-- --nostringval-- --nostringval--
[Edit: I fixed the typo from -SOutputFile to -sOutputFile to avoid this red herring. (But that is what some of the comments/answers are referring to.)]
回答1:
This worked for me...
gs \
-sDEVICE=pdfwrite \
-dCompatibilityLevel=1.4 \
-dPDFSETTINGS=/printer \
-dNOPAUSE \
-dQUIET \
-dBATCH \
-sOutputFile=output.pdf \
input.pdf
Edited by -kp-
To spell it out explicitly (and to re-iterate what KenS wrote in his comment):
-SOutputFile=...
does NOT work-sOutputFile=...
is the correct syntax. (Ghostscript command line parameters are case sensitive!)
Also, with recent versions of Ghostscript, you can now use -o output.pdf
instead of the long version. -o ...
also automatically and implicitely sets the -dBATCH -dNOPAUSE
parameters. So the shortest way to write this command is:
gs \
-sDEVICE=pdfwrite \
-dCompatibilityLevel=1.4 \
-dPDFSETTINGS=/printer \
-q \
-o output.pdf \
input.pdf
回答2:
It could be that you have simply mixed up your input and output filenames. I have done that before and got the same message. It's easy to do, since the output file command comes before the input file.
来源:https://stackoverflow.com/questions/27454350/ghostscript-unrecoverable-error-undefinedfilename-in-setpagedevice