Skip to content

Instantly share code, notes, and snippets.

@thillsd
Created February 18, 2025 09:50
Show Gist options
  • Save thillsd/d8a2b606fbb6ce8acd2749c77fbd33f4 to your computer and use it in GitHub Desktop.
Save thillsd/d8a2b606fbb6ce8acd2749c77fbd33f4 to your computer and use it in GitHub Desktop.
@echo off
setlocal enabledelayedexpansion
:: Check if arguments are passed
if "%~1"=="" (
echo Drag and drop Word files onto this script or provide file paths as arguments.
pause
exit /b
)
:: Loop through all provided file arguments
for %%F in (%*) do (
set "inputFile=%%~F"
set "tempFile=%%~dpnF-temp.pdf"
set "finalFile=%%~dpnF.pdf"
set "processedFile=%%~dpnF-processed.pdf"
echo Converting "!inputFile!" to "!tempFile!"...
powershell -NoProfile -ExecutionPolicy Bypass -Command ^
"& { $word = New-Object -ComObject Word.Application; $doc = $word.Documents.Open('!inputFile!', [Type]::Missing, $true); $doc.SaveAs([ref]'!tempFile!', 17); $doc.Close($false); $word.Quit(); }"
if exist "!tempFile!" (
echo Successfully converted: "!tempFile!"
:: Apply ImageMagick 'magick convert'
echo Running ImageMagick 'magick convert'...
magick convert -render -density 300 "!tempFile!" "!finalFile!"
if exist "!finalFile!" (
echo Running Ghostscript 'gs'...
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH -sOutputFile="!processedFile!" "!finalFile!"
move /Y "!processedFile!" "!finalFile!"
if exist "!finalFile!" (
echo Successfully processed "!finalFile!"
del "!tempFile!" 2>nul
echo Cleanup complete.
) else (
echo Ghostscript conversion failed.
)
) else (
echo ImageMagick conversion failed.
)
) else (
echo Failed to convert: "!inputFile!"
)
)
echo All tasks completed.
pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment