开发者

GhostScript Batch Mode?

开发者 https://www.devze.com 2023-04-04 21:47 出处:网络
I\'m using GhostScript to convert PDFs to PNGs, the problem is that for each page I\'m calling: gs -sDEVICE=pnggray -dBATCH -dNOPAUSE -dFirstPage=10 -dLastPage=10 -r600 -sOutputFile=image_10.png pdf_

I'm using GhostScript to convert PDFs to PNGs, the problem is that for each page I'm calling:

gs -sDEVICE=pnggray -dBATCH -dNOPAUSE -dFirstPage=10 -dLastPage=10 -r600 -sOutputFile=image_10.png pdf_file.pdf

Which is not good, I want to pass dFirstPage=10 dLastPage=30 for example and make GhostScript automatically extract each page in a SEPARATE png file WITH the PAGE-NUMBER in the filename, without starting it again with different sOutputFile...

开发者_StackOverflow社区

I know it's probably something simple but I'm missing it...

Also, it would be great if someone can tell me what parameter I need to pass to make ghostscript run in total silence, without any output to the console.

EDIT: Adding %d to the output parameter adds the number of the run, instead of the number of the page. For example:

-dFirstPage=10 -dLastPage=15 -sOutputFile=image_%d.png

results in:

image_1.png, image_2.png, image_3.png etc... instead of: image_10.png, image_11.png, image_12.png ...


Save this as a file

 #!/bin/bash
 case $# in [!3] ) printf "usage : ${0##*/} stPage endPage File\n" >&2 ;; esac
 stPage=$1
 endPage=$2
 (( endPage ++ ))
 file=$3
 i=$stPage
 while (( i < endPage  )) ; do
    gs  -sstdout=/dev/null -sDEVICE=pnggray -dBATCH -dNOPAUSE -dPage=$i -r600 -sOutputFile=image_$i.png ${file}

   (( i ++ ))
 done

Check in the ghost script manual to see if there is a -dPage=${num} option, else use -dFirstPage=${i} -dLastPage=${i} .

Then make it executeable chmod 755 batch_gs.sh

Finally run it with arguments

  batch_gs.sh 3 5 fileName

(Lightly tested).

I hope this helps.


Unfortunately, what you want to do is not possible. See also my answers here and here.

If you want to do all PNG conversions in one go (without restarting Ghostscript for each new page), you have to live with the fact, that the %d macro always starts with numbering the first output page as 1, but of course you will gain a much better performance.

If you do not like this naming conventions in your end result, you have to do a second step that renames the resulting files to their final name.

Assuming your initial output files are named image_1.png ... image_15.png, but you want them named image_25.png ... image_39.png, your core command to do this would be:

 for i in $(seq 1 15); do
     mv image_${i}.png image_$(( ${i} + 24)).png
 done

Note, this will go wrong if the two ranges of numbers intersect, as the command would then overwrite one of your not-yet-renamed input files. To be save, don't use mv but use cp to make a copy of the new files in a temporary subdirectory first:

 for i in $(seq 1 15); do
     cp -a image_${i}.png temp/image_$(( ${i} + 14)).png
 done
0

精彩评论

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