I've installed Ghostscript on Windows as what I'm looking to do is compress/reduce the size of 12,000+ PDF files on a network share. This wouldn't be possible with any GUI software as it just bombs out after a while due to running out of resources, so I think command line is the way to go here.
I've read the Ghostscript documentation and different examples of compressing PDF files, but I can't seem to find anything that operates as a large batch.
Basically, I'd need to target multiple folders to recursively compress the files which will be on the network share.
Is this possible to do so with Ghostscript? If so, please advise with some command examples to help me acheive this.
Thanks!
With the following script you can define all directories needed in the array variable filesDir
.
It will loop over all these directories and search for all pdf files in all directories including subdirectories.
For all found pdf files it will use this ghostscript command (GitHub) and output the file with name e.g. fileabc.pdf
with a new name: compr_fileabc.pdf
.
Edit #1:
I changed the script as requested by the comments to either write new pdf files or overwrite the input pdf file. To select between these to options change the createNewPDFs
variable to 1 (new files) or 0 (overwrite).
Because ghostscript can't write to the input file the output file will be written at the users temporary path (%TEMP%
) and moved to the original input file to overwite this file. It will only overwrite the input pdf file if the new file is smaller in size.
Further the ghostscript
command is substituted by a variable with the same name because under Windows it can be either gswin64c
(64 bit) or gswin32c
(32 bit).
If the outcomming sizes are not small enough play with these ghostscript command switch: -dPDFSETTINGS=/printer
, it is explained below.
Batch script:
@echo off
setlocal EnableDelayedExpansion
rem ghostscript executable name
set "ghostscript=gswin64c"
rem directories to scan for files
set "filesDir[0]=FOLDER1"
set "filesDir[1]=FOLDER2"
set "filesDir[2]=FOLDER3"
rem extension of files to be scanned
set "ext=pdf"
rem new file be creation or input file overwrite
set "createNewPDFs=0"
rem file prefix for new files (if they should be created)
set "filepre=compr_"
rem loop over all directories defined in filesDir array
for /f "tokens=2 delims==" %%d in ('set filesDir[') do (
if exist "%%~d" (
pushd "%%~d"
rem loop over all files in all (sub)directories with given extension
for /f "delims=*" %%f in ('dir "*.%ext%" /b /s /a:-d') do (
if [%createNewPDFs%] EQU [1] (
%ghostscript% -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/printer -dNOPAUSE -dQUIET -dBATCH -sOutputFile="%%~dpf%filepre%%%~nxf" "%%~f"
) else (
%ghostscript% -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/printer -dNOPAUSE -dQUIET -dBATCH -sOutputFile="%TEMP%\%%~nxf" "%%~f"
for %%t in ("%TEMP%\%%~nxf") do ( set "newSize=%%~zt" )
for %%t in ("%%~f") do ( set "oldSize=%%~zt" )
if [!newSize!] LSS [!oldSize!] (
rem new file is smaller --> overwrite
move /y "%TEMP%\%%~nxf" "%%~f"
) else (
rem new file is greater --> delete it of the temp dir
del "%TEMP%\%%~nxf"
)
)
)
popd
)
)
Found GitHub ghostscript command to reduce pdf size:
This can reduce files to ~15% of their size (2.3M to 345K, in one case) with no obvious degradation of quality.
ghostscript -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/printer -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf
Other options for PDFSETTINGS:
- /screen selects low-resolution output similar to the Acrobat Distiller "Screen Optimized" setting.
- /ebook selects medium-resolution output similar to the Acrobat Distiller "eBook" setting.
- /printer selects output similar to the Acrobat Distiller "Print Optimized" setting.
- /prepress selects output similar to Acrobat Distiller "Prepress Optimized" setting.
- /default selects output intended to be useful across a wide variety of uses, possibly at the expense of a larger output file.
Source: http://ghostscript.com/doc/current/Ps2pdf.htm
Command reference links from ss64.com:
I don't know if anyone need it, but here's my command to highly compress PDF files with no degradation of quality. I've found it by method of many trials and errors an it's greatly reduce PDF files size. P.S. Sorry for posting not in the above thread, but I don't enough reputation for it as a newcomer.
%ghostscript% -q -dNOPAUSE -dBATCH -dSAFER -dSimulateOverprint=true -sDEVICE=pdfwrite -dPDFSETTINGS=/ebook -dEmbedAllFonts=true -dSubsetFonts=true -dAutoRotatePages=/None -dColorImageDownsampleType=/Bicubic -dColorImageResolution=150 -dGrayImageDownsampleType=/Bicubic -dGrayImageResolution=150 -dMonoImageDownsampleType=/Bicubic -dMonoImageResolution=150 -sOutputFile=output.pdf input.pdf
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With