使用Ghostscript服务器模式将PDF转换为PNG

6

我能够将PDF的特定页面转换成PNG,代码如下:

gs \
  -dSAFER \
  -dBATCH \
  -dNOPAUSE \
  -sDEVICE=png16m \
  -dGraphicsAlphaBits=4 \
  -sOutputFile=gymnastics-20.png \
  -dFirstPage=20 \
  -dLastPage=20 \
   gymnastics.pdf

我想知道是否可以使用Ghostscript的JOBSERVER模式,在不每次启动Ghostscript时处理多个转换,以避免成本。
来源:http://pages.cs.wisc.edu/~ghost/doc/svn/Use.htm

-dJOBSERVER

Define \004 (^D) to start a new encapsulated job used for compatibility with Adobe PS Interpreters that ordinarily run under a job server. The -dNOOUTERSAVE switch is ignored if -dJOBSERVER is specified since job servers always execute the input PostScript under a save level, although the exitserver operator can be used to escape from the encapsulated job and execute as if the -dNOOUTERSAVE was specified.

This also requires that the input be from stdin, otherwise an error will result (Error: /invalidrestore in --restore--).

Example usage is:

   gs ... -dJOBSERVER - < inputfile.ps
                -or-
   cat inputfile.ps | gs ... -dJOBSERVER - 

Note: The ^D does not result in an end-of-file action on stdin as it may on some PostScript printers that rely on TBCP (Tagged Binary Communication Protocol) to cause an out-of-band ^D to signal EOF in a stream input data. This means that direct file actions on stdin such as flushfile and closefile will affect processing of data beyond the ^D in the stream.

这个想法是在进程中运行Ghostscript。脚本将接收来自PDF的请求,生成指定的图片时会使用Ghostscript。我不太想每次都启动一个新的Ghostscript进程。

你尝试过我的解决方案来解决你的问题了吗? - Kurt Pfeifle
1个回答

2
所以为什么你不能简单地使用这样一个命令:
gs \
  -sDEVICE=png16m \
  -dGraphicsAlphaBits=4 \
  -o pngimages_%03d.png \
   \
  -dFirstPage=20 \
  -dLastPage=20 \
   gymnastics.pdf
   \
  -dFirstPage=3 \
  -dLastPage=3 \
   sports.pdf
   \
  -dFirstPage=33 \
  -dLastPage=33 \
   athletics.pdf
   \
  -dFirstPage=4 \
  -dLastPage=4 \
   lazyness.pdf

这将一次性从多个PDF生成多张PNG图像。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接