0
votes

i want to convert all pdfs of folder to png images,like if i have two pdfs in folder name test1.pdf and test2.pdf with two pages,then it should generate test1-1.png ,test1-2.png,test2-1.png,test2-2.png. I am using this command line ,what should i place instead of '#-%d.png'?

gs -dNOPAUSE -dBATCH -sDEVICE=png16m -sOutputFile="/var/www/pdf_png/pdfs/#-%d.png" /var/www/pdf_png/pdfs/*.pdf

2

2 Answers

2
votes

You can't do it using Ghostscript alone, you need to write a shell script to invoke Ghostscript on each source file.

This question has a script in it that you may be able to adapt for your purposes

1
votes
find . -type f -iname "*.pdf" -exec sh -c 'gs -dNOPAUSE -dBATCH -sDEVICE=pngalpha -r300 -sOutputFile=$1.png $1' x {} \;

Note: This will still output FILENAME.pdf.png, but hey ... it's a one liner ...

Explanation

  • find command finds all pdf files and passes them to sh via -exec
  • the {} represents the file name, but in order to be used twice in the command it has to be passed with the sh -c trick as a positional argument
  • in case you are wondering the character x is passed as the $0 argument because convetionally $0 is the shellname
  • \; is the character used to signal the end of the command