Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ghostscript in a Windows .bat file to convert multiple pdf files to png

I have many many pdf files in a directory that I need to convert from pdf to png. Currently, I am using the ImageMagick command: magick mogrify -format png *.pdf

Because, there are so many files, I would like to use ghostscript directly because there are several sources that suggest that I could achieve a 75% reduction in processing time by doing this.

However, I am having trouble finding a clean dos command example to accomplish the same thing as the ImageMagick command above. I believe I need to execute the gswin64c.exe module but I am unsure how to do this to accomplish what I need to get done. Can someone provide me with a clean example of the ghostscript that accomplishes what I'm doing in ImageMagick?

like image 983
Elliott Avatar asked Jun 13 '17 22:06

Elliott


1 Answers

After much digging, what I discovered was that ghostscript does not really have a wildcard that would allow reference to all files of a certain pattern (like ImageMagick does). To convert all files in a directory that are pdf's to png's, a dos script like the following could be used:

 for %%x in (*)  do gswin64c.exe -sDEVICE=png16m -dBATCH -dNOPAUSE -dQUIET -
       SOutputFile="%%~nx.png" %%~nx.pdf

This can also be run from the command line by simply using single percentage signs (%) instead of the double percentage signs in the script above.

The terms are as follows:

gswin64c.exe: This is the dos command version of GhostScript. It should be used as opposed to gswin64.exe which will open a GhostScript window.

-sDEVICE=png16m This indicates the form of the output file. Is this case png.

-dBATCH -dNOPAUSE. These are GhostScript options and when employed will allow for continuous operation of the script (without them, the program will pause after each file converted).

-dQUIET - This suppresses notifications that display on stdout after each processed file.

SOutputFile="%%~nx.png" %%~nx.pdf This indicates the pattern for the input files and the output files. x is the loop variable. The % sign is used as a wild card. ~nx is a Dos convention which truncates the extension of an echoed file name.

like image 124
Elliott Avatar answered Oct 31 '22 18:10

Elliott