7

I'm using Ghostscript to rasterize the first page of a PDF file to JPEG. To avoid creating tempfiles, the PDF data is piped into Ghoscripts's stdin and the JPEG is "drained" on stdout. This pipeline works like a charm until GS receives invalid PDF data: Instead of reporting all error messages on stderr as I would have expected, it still writes some of the messages to stdout instead.

To reproduce:

$ echo "Not a PDF" >test.txt
$ /usr/bin/gs -q -sDEVICE=jpeg -dBATCH -dNOPAUSE -dFirstPage=1 -dLastPage=1 \
    -r300 -sOutputFile=- - < test.txt 2>/dev/null
Error: /undefined in Not
Operand stack:

Execution stack:
...

Note the 2>/dev/null above does not suppress the error messages. Ghostscript's documentation already warned that writing to stdout requires the -q flag to suppress messages on stdout, but I still seem to be missing something here.

Daniel Werner
  • 1,350
  • 16
  • 26

1 Answers1

15

If you want to really silence Ghostscript, modify your command line like this:

/usr/bin/gs -q        \
     -sstdout=%stderr \
     -sDEVICE=jpeg    \
     -dBATCH          \
     -dNOPAUSE        \
     -dLastPage=1     \
     -r300            \
     -sOutputFile=-   \
     - < test.txt 2>/dev/null

The addition of -sstdout=%stderr allows Postscript stdout to be redirected, while still allowing drivers to write to stdout. (That patch is in Ghostscript since ~2001, Sept 22.)

Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
  • 2
    Thanks, pipitas -- this is exactly what I was looking for. I had to explicitely specify `-sstdout=/dev/null`, however, since `%sstderr` would create a like-named file in the current directory. For future reference, here is the original mailing list thread discussing the patch: http://www.ghostscript.com/pipermail/gs-code-review/2001-March/000273.html – Daniel Werner Aug 03 '10 at 08:12
  • 1
    There's a typo over there. It should be `%stderr` without the double s – Edward B Jul 09 '15 at 22:28
  • how to silence ghostscript in windows ? – user889030 Apr 16 '21 at 14:44
  • 1
    @user889030: Same command line, bug replace *`/usr/bin/gs`* by *`\path\to\gswin64c.exe`* and *`/dev/null`* by *`nul`*. – Kurt Pfeifle Apr 18 '21 at 14:56