Skip to content

Instantly share code, notes, and snippets.

@jhwilson
Last active September 17, 2023 18:32
Show Gist options
  • Save jhwilson/8a323fe95d662b330e89dce64f3a41ef to your computer and use it in GitHub Desktop.
Save jhwilson/8a323fe95d662b330e89dce64f3a41ef to your computer and use it in GitHub Desktop.
pdf compression
@echo off
setlocal
:: Prompt user to select a PDF using PowerShell
for /f "delims=" %%i in ('powershell -command "[System.Reflection.Assembly]::LoadWithPartialName('System.windows.forms') | Out-Null; $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog; $OpenFileDialog.InitialDirectory = [Environment]::GetFolderPath('Desktop'); $OpenFileDialog.Filter = 'PDF Files (*.pdf)|*.pdf'; $OpenFileDialog.ShowDialog() | Out-Null; $OpenFileDialog.FileName"') do set "pdfPath=%%i"
:: Exit if no file was selected
if "%pdfPath%"=="False" (
echo No file selected. Exiting...
exit /b
)
:: Get the directory and base name of the selected PDF
for %%F in ("%pdfPath%") do (
set "dirPath=%%~dpF"
set "baseName=%%~nF"
)
:: Set the output filename with "-compressed.pdf"
set "outputPath=%dirPath%%baseName%-compressed.pdf"
:: Call Ghostscript to compress the PDF
"C:\Program Files\gs\gsX.XX\bin\gswin64c.exe" -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/prepress -dNOPAUSE -dQUIET -dBATCH -sOutputFile="%outputPath%" "%pdfPath%"
echo Compression complete. The compressed file has been saved as: %outputPath%
pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment