Skip to content

Instantly share code, notes, and snippets.

@cwg999
Created August 3, 2017 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cwg999/fb1c554dc1f0d2b71b1ec6f29b719e56 to your computer and use it in GitHub Desktop.
Save cwg999/fb1c554dc1f0d2b71b1ec6f29b719e56 to your computer and use it in GitHub Desktop.
Compresses PDFs using a ghostscript batch file, (required since ghostscript doesn't like powershell)
$ArrayList = [System.Collections.ArrayList]@()
Get-ChildItem -Path .\ -Recurse | Where-Object {
$_ -is [System.IO.FileInfo] `
-and ($_.Extension.ToLower().CompareTo('.pdf') -eq 0) `
-and ($_.name.CompareTo('out.pdf') -ne 0)
} | ForEach-Object {
$ArrayList.add($_.FullName)
}
workflow Test-Workflow{
param(
[Parameter (Mandatory = $true)]
[System.Collections.ArrayList]$ArrayList
)
ForEach -Parallel -throttlelimit 5 ($fileFullName in $ArrayList)
{
$GSBATCHPATH=".\gswin64c.bat"
$file = Get-Item $fileFullName
$guid = [guid]::NewGuid()
# Write-Output 'OBJECT: ' $tempFolder
$tempFolder = Join-Path -Path $file.Directory -ChildPath $guid
# Write-Output 'tempFolder: ' $tempFolder
$outputFilePath = Join-Path -Path $tempFolder -ChildPath 'out.pdf'
# Write-Output 'outputFilePath: ' $outputFilePath
# Write-Host '$file.FullName: ' $file.FullName
# Write-Host 'outputFilePath: ' $outputFilePath
$folder = New-Item $tempFolder -type directory
$command = '"'+$GSBATCHPATH+'" "'+ $file.FullName+'" "'+$outputFilePath+'"'
$output = iex "& $command"
$item = Get-Item $outputFilePath
if($item.Length -gt 25600){
Copy-Item $outputFilePath -Destination $file.FullName
Write-Output 'Completed ' $file.FullName
}else{
Write-Output 'WARNING: ' $file.FullName ' did not compress successfully.'
}
Remove-Item $tempFolder -Recurse
}
}
Test-Workflow -ArrayList ($ArrayList)
Write-Host "Press any key to continue ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Write-Host "Finished"
#GSWIN64C.BAT
#set INPUT=%1
#set OUTPUT=%2
#"C:\Program Files (x86)\gs\gs9.21\bin\gswin32c.exe" -q -dNOPAUSE -dBATCH -dSAFER ^
# -sDEVICE=pdfwrite ^
# -dCompatibilityLevel=1.3 ^
# -dPDFSETTINGS=/screen ^
# -dEmbedAllFonts=true ^
# -dSubsetFonts=true ^
# -dColorImageDownsampleType=/Bicubic ^
# -dColorImageResolution=300 ^
# -dGrayImageDownsampleType=/Bicubic ^
# -dGrayImageResolution=300 ^
# -dMonoImageDownsampleType=/Bicubic ^
# -dMonoImageResolution=300 ^
# -sOutputFile=%OUTPUT% %INPUT%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment