防止Ghostscript将错误写入标准输出
我正在使用Ghostscript将PDF文件的首页光栅化为JPEG.为避免创建临时文件,将PDF数据通过管道传递到Ghoscripts的stdin中,而将JPEG在stdout中排空".在GS接收到无效的PDF数据之前,该管道就像一个咒语一样起作用:它并没有像我期望的那样在stderr上报告所有错误消息,而是将一些消息写到了 stdout .
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.
要复制:
$ 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:
...
请注意,上面的2>/dev/null
不会抑制错误消息. Ghostscript的文档已经警告说,向stdout进行写入需要-q
标志来禁止显示stdout上的消息,但是我似乎仍然在这里遗漏了一些东西.
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.
如果您想真的使Ghostscript静音,请按如下所示修改您的命令行:
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
添加的-sstdout=%stderr
允许重定向Postscript stdout,同时仍允许驱动程序写入stdout. (该补丁自9月22日〜2001年以来一直存在于Ghostscript中.)
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.)