views:

35

answers:

1

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.

+2  A: 

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

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

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

pipitas
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