daknetworks.com

You are here: Blog Compress PDF With Ghostscript On Windows

Compress PDF With Ghostscript On Windows

Compress PDF with Ghostscript On Windows

DOWNLOAD & INSTALL GHOSTSCRIPT

You can download Ghostscript here (get the 64-bit version):
https://www.ghostscript.com/download/gsdnld.html

Installation is easy but the installer doesn't put the directory in the PATH. Until that time, you will have to type in the whole path to run the program:
C:\Program Files\gs\gs9.21\bin\gswin64c.exe

Adding to the PATH allows you to run the program by just using:
gswin64c.exe

To change the PATH temporarily, you can add to the PATH by typing in the command line:
set PATH=%PATH%;C:\Program Files\gs\gs9.21\bin\;C:\Program Files\gs\gs9.21\lib\

Or you can:

  • -right-click MY-COMPUTER/
  • -click PROPERTIES
  • -click ADVANCED-SYSTEM-SETTINGS
  • -click ENVIRONMENTAL-VARIABLES (at the bottom-right).
  • -in the lower section called "SYSTEM VARIABLES", find PATH
  • -click EDIT
  • -find VARIABLE VALUE
  • -keep everything there
  • -go to the end of the value
  • -add the following:
    ;C:\Program Files\gs\gs9.21\bin\;C:\Program Files\gs\gs9.21\lib\;

NOTE: do not remove any of the existing values.

RUNNING GHOSTSCRIPT

The idea here is that Ghostscript will create PDF's for you without step-by-step interaction. Let's say you have a directory of PDF that somebody scanned at 1200dpi with each PDF at 10MB. After time, this directory becomes entirely too large. We can use Ghostscript to re-compress the PDF's by 90% and take each PDF down to 1MB.

Ghostscript is suite of commands and not just one command. The command we are interested in is: ps2pdf

To run for a single file:
ps2pdf -dPDFSETTINGS#/ebook C:\path\to\input\file.pdf c:\path\to\output\file.pdf

There are a bunch of options but the most are correctly set by default:
https://www.ghostscript.com/doc/current/Ps2pdf.htm

Here is a script to run for an entire directory. Create the batch file and name it compress-all.bat. Put the batch file in the directory for which you want to compress files. Run the batch file from command line. It will create a "compressed" folder and put a copy of the compressed files in there:
=====

@echo off
setlocal
set GS_OUTPUT_DIR=compressed
mkdir %GS_OUTPUT_DIR% 
for %%i in (*.pdf) do ps2pdf -dPDFSETTINGS#/ebook "%%i" "%GS_OUTPUT_DIR%\%%i"

Contact Dak Networks

We are not taking on new clients at this time.