防止Ghostscript将错误写入标准输出 [英] Prevent Ghostscript from writing errors to standard output

查看:101
本文介绍了防止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.)

这篇关于防止Ghostscript将错误写入标准输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆