Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Batch file to convert PDFs to JPGs (requires GraphicsMagick and Ghostscript installed)
:: pdf-to-jpg.bat
:: This batch file converts all the PDF files in a folder to JPGs,
:: and each page is a separate, numbered JPG.
:: 1. Make sure GraphicsMagick and Ghostscript are installed and in your PATH.
:: 2. Save this pdf-to-jpg.bat file to the folder alongside the files you want to convert.
:: 3. Double-click pdf-to-jpg.bat.
:: Don't show these commands to the user
@ECHO off
:: Set the title of the window
TITLE Convert PDF pages to JPGs
:: Ask for color space
SET colorspace=CMYK
SET /p colorspace=What colorspace, RGB or CMYK? Default is CMYK.
:: Ask for image width
SET imagewidth=1280
SET /p colorspace=Set image width in pixels. Default is 1280.
:: Ask for image resolution
SET resolution=300
SET /p colorspace=Set image resolution in pixels per inch. Default is 300.
:: Ask if we want to convert all the files in the folder, or one by one
SET option=
SET /p option=Hit return to convert all the files in this folder. Or type the filename of the file you want to convert.
IF "%option%"=="" GOTO batchconvert
:: Okay, we're doing one file at a time
ECHO Saving "%option%" as JPG...
SET optionNoFileExtension="%option:~0,-4%"
gm convert -colorspace %colorspace% -quality 100 -geometry %imagewidth%x -density %resolution% -units PixelsPerInch "%option%" +adjoin "%optionNoFileExtension%-%%d.jpg"
SET option=
SET /p option=Done. Hit return to exit, or enter the name of another file to convert.
IF "%option%"=="" GOTO:eof
GOTO singleconvert
:: A batch conversion
:: Tell the user what's happening
ECHO Saving all PDF files in this folder as JPGs...
:: Loop through all files in the folder and convert -- except for this file
FOR %%f in (.\*.pdf) DO (
:: Note: %%~nf is 'filename with no file extension'
gm convert -colorspace %colorspace% -quality 100 -geometry %imagewidth%x -density %resolution% -units PixelsPerInch "%%f" +adjoin "%%~nf-%%d.jpg"
:: Let the user choose to exit or run again
SET repeat=
SET /p repeat=Hit return to run again, or any other key and enter to exit.
IF "%repeat%"=="" GOTO batchconvert
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.