开发者

Using Ghostscript in server mode to convert PDFs to PNGs

while i am able to convert a specific page of a PDF to a PNG like so:

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

i am wondering if i can somehow use ghostscript's JOBSERVER mode to process several conversions without having to incur the cost of starting up ghostscript each time.

from: 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 std开发者_StackOverflow社区in, 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.

the idea is to run ghostscript in-process. the script would receive a request for a particular page of a pdf and would use ghostscript to generate the specified image. i'd rather not start up a new ghostscript process every time.


So why can't you simply use a command like this:

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

This will generate several PNG images from different PDFs in a single go.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜