PDF Shrink

I found a useful PDF shrinking script at http://www.alfredklomp.com/programming/shrinkpdf/. Unfortunately it was a bash script, and I could not use it in its current state, so I created a batch file version of it.

Not only did I convert it, I upgraded it. In addition to shrinking the PDF, you can drag and drop multiple PDFs onto the BAT file and they will be shrunk and placed in a new sub-directory.

The script requires Ghostscript which can be found here, http://www.ghostscript.com/download/. You may need to change the path to the Ghostscript executable on your computer.

</pre>
@echo off

REM Shrinks PDFs and puts them in a subdirectory of the first file
REM Usage: shrinkpdf.bat file1 file2 ...

REM Location of the Ghostscript exectuble
set GSPATH="C:\Program Files (x86)\GPLGS\gswin32c.exe"

REM Name of the subdirectory to store the shrunk files to
set OUTSUBDIR=shrunk

if "%~1"=="" (
 echo Usage: shrinkpdf.bat file1 file2 ...
 goto end
)

REM Take the path of the first file and use it as the folder
REM to create the shrunk directory under
set OUTDIR=%~dp1\%OUTSUBDIR%

REM Create the output folder if it doesn't exist
if not exist "%OUTDIR%" mkdir "%OUTDIR%"

:shrinkPDF
set OUTNAME=%OUTDIR%\%~nx1

@echo "Processing %~nx1"

REM Work the shrinking magic
REM Based on http://www.alfredklomp.com/programming/shrinkpdf/
%GSPATH% -q -dNOPAUSE -dBATCH -dSAFER ^
-sDEVICE=pdfwrite ^
-dCompatibilityLevel=1.3 ^
-dPDFSETTINGS=/screen ^
-dEmbedAllFonts=true ^
-dSubsetFonts=true ^
-dColorImageDownsampleType=/Bicubic ^
-dColorImageResolution=72 ^
-dGrayImageDownsampleType=/Bicubic ^
-dGrayImageResolution=72 ^
-dMonoImageDownsampleType=/Bicubic ^
-dMonoImageResolution=72 ^
-sOutputFile="%OUTNAME%" ^
"%~1"

REM Load the next file
shift

REM Are there any more files to shrink?
if "%~1"=="" goto end

goto shrinkPDF

:end
<pre>

6 thoughts on “PDF Shrink

  1. Pingback: How to shrink / compress pdf files? | Tips Thoughts Notes

  2. Karthik

    Hi Christopher,

    Thanks for posting this code. I am getting the following recurrent error when trying to compress a file –

    Subsample filter does not support non-integer downsample factor
    Failed to initialise downsample filter, downsampling aborted.
    The only Downsample filter for monochrome images is Subsample, ignoring request.

    I have set the path correctly to the executable gswin32c.exe and saved it in a bat file to execute.

    I am not running into any helpful links on querying through error. Any help would be much appreciated.

    Thanks

    Reply
    1. Christopher Post author

      It sounds like you are correctly pointed to gswin32c. Maybe your version of gswin32c is newer.

      This page has the description of gs2pdf executable, http://www.ghostscript.com/doc/current/Ps2pdf.htm.

      Maybe remove the line “-dMonoImageDownsampleType=/Bicubic ^” or change “/Bicubic” in that line to “/Subsample.” It looks like 1.5 is the default downsample value, so you might add a line
      “-MonoImageDownsampleThreshold=2”.

      Reply
  3. Octavius

    I installed GS 9.21 64 and downloaded the script as a ANSI format .bat file. I changed the location to the gswin64c.exe file correctly, and when I try dragging a pdf file onto the bat file, the cmd prompt just flashes and goes away without doing anything. Can you help?

    Reply
    1. Christopher Post author

      I would suggest that you either try running it from the command prompt to set what it’s response or append “pause” to the end of the bat file and see what it says after.ypu run it again. The pause command will require you to press a key before closing the window.

      Let me know what the error is.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *