Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save danzig666/c01cb130537b676ff548b1f45d485c14 to your computer and use it in GitHub Desktop.
Save danzig666/c01cb130537b676ff548b1f45d485c14 to your computer and use it in GitHub Desktop.
Rasterize PDF files with Ghostscript and ImageMagick - Windows batch file version
@echo off
REM Rasterize PDF files with Ghostscript and ImageMagick.
setlocal
set DENSITY=600
set ADDEXT=.raster
REM parse options
if "%~1"=="-d" (
set DENSITY=%~2
shift /1
shift /1
)
if "%~1"=="-e" (
set ADDEXT=%~2
shift /1
shift /1
)
REM check if a file is specified
if "%~1"=="" (
echo Target filename argument is required.
exit /b
)
REM create a temporary directory
for /f %%a in ('echo %TEMP%\tmp%random%') do (
set TMPDIR=%%a
)
mkdir %TMPDIR%
REM convert all specified files
:loop
if "%~1"=="" goto end
set file=%~1
gs -dNOPAUSE -r%DENSITY% -dBATCH -sDEVICE=png16m -sOutputFile="%TMPDIR%\tmp-%%02d.png" "%file%"
REM remove profile to avoid 'Insufficient data for an image' error
C:\Util\ImageMagick-7.1.0-portable-Q8-x64\convert.exe "%TMPDIR%\tmp-*.png" +profile '*' "%~n1%ADDEXT%.pdf"
del "%TMPDIR%\tmp-*.png"
shift /1
goto loop
:end
REM remove temporary directory
rmdir /s /q %TMPDIR%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment