Efficient PDF optimization with Ghostscript CLI
To reduce a PDF’s size with Ghostscript, create a separate candidate and compare it with the
original before sharing it. The /ebook preset is a starting point for documents with large
images, but a successful conversion can also produce a larger file. This walkthrough keeps both
files and gives you a way to decide whether the candidate is useful.
Decide whether rewriting the PDF is appropriate
Ghostscript’s pdfwrite device builds a new PDF from the input’s drawing operations. It does not
simply compress the original bytes. Artifex explains why this can change document features and
increase file size.
Use this workflow for a disposable sharing copy of a document whose appearance you can check. Keep signed originals: the rewritten copy does not retain their valid digital signatures. If you need fillable forms, accessibility tags and reading order, attachments, or archival conformance, use a workflow that explicitly preserves and validates those features. Looking at rendered pages cannot establish that they survived. Links, bookmarks, searchable text, and metadata also need checking when you rely on them.
Set up Ghostscript
You need Ghostscript, Poppler’s pdfinfo and pdftoppm, Bash, and a PDF viewer. On Ubuntu, the
packages are named ghostscript and poppler-utils. Use maintained packages and record your
versions:
gs --version && pdfinfo -v && pdftoppm -v
The commands below were tested on Linux with Ghostscript 10.07.1 and Poppler 26.08.0, and with Ubuntu 24.04’s Ghostscript 10.02.1 and Poppler 24.02.0. They are Bash examples; Windows and macOS execution is not covered here.
Create a candidate without overwriting a previous result
Work in a directory containing your PDF under the name original.pdf. Keep that file throughout
the comparison. Run this whole block in Bash, including the parentheses:
(
set -eu
pdfinfo ./original.pdf
mkdir ./pdf-review
gs -sDEVICE=pdfwrite -dPDFSETTINGS=/ebook \
-dCompatibilityLevel=1.7 -dAutoRotatePages=/None \
-dNOPAUSE -dBATCH -dPDFSTOPONERROR \
-sOutputFile=./pdf-review/candidate.pdf -f ./original.pdf
)
The initial pdfinfo checks that Poppler can read the input. mkdir deliberately has no -p:
if pdf-review already exists, the block stops before Ghostscript runs, even without an
interactive terminal. Your previous result and original.pdf stay intact. The parentheses keep
set -eu scoped to this block.
-sDEVICE=pdfwrite selects PDF output. /ebook can reduce image detail; PDF 1.7 and disabled
automatic page rotation are explicit choices for this trial. -dNOPAUSE -dBATCH avoids page
prompts and exits after processing. -dPDFSTOPONERROR asks Ghostscript to stop on PDF errors
instead of attempting its usual recovery, as described in the
usage documentation.
Neither program is a complete PDF validator. Read any warnings, and if conversion fails, treat
anything left in pdf-review as incomplete.
Compare file sizes
After conversion succeeds, compare actual bytes and page counts:
wc -c ./original.pdf ./pdf-review/candidate.pdf
pdfinfo ./original.pdf && pdfinfo ./pdf-review/candidate.pdf
The first two wc rows are the individual sizes; the total row adds them. In the two pdfinfo
reports, compare Pages and Page size. A lower byte count does not excuse a missing page or an
unexpected size change. For documents with mixed page sizes, also check each page in your viewer.
Render both documents with Poppler, independently of Ghostscript:
(
set -eu
mkdir ./pdf-review/previews
pdftoppm -r 150 -png ./original.pdf ./pdf-review/previews/original
pdftoppm -r 150 -png ./pdf-review/candidate.pdf ./pdf-review/previews/candidate
)
This writes one PNG per page, with matching original- and candidate- prefixes. It refuses to
reuse an existing previews directory. Open corresponding images side by side and inspect small
text, fine lines, photographs, color, and page orientation. These 150 dpi previews help find
visible changes; also open the PDFs at your intended viewing zoom or proof them at the intended
print size. Try selecting and searching text and following important links in the candidate.
Use the candidate only if it is smaller and passes the checks your document needs. Otherwise,
keep using the original. Already compact text or vector PDFs may grow, and a heavily compressed
scan may have little left to save. To try another setting, rename the review directory to retain
that trial, then rerun from original.pdf. Recompressing a lossy candidate can discard more detail.
PDF optimization presets
These are the color and grayscale downsampling targets in the tested versions. Ghostscript’s preset definitions also specify separate settings for monochrome images:
| Preset | Color and grayscale target | What to watch |
|---|---|---|
/screen | 72 dpi | Fine image detail can disappear. |
/ebook | 150 dpi | Check scans and small text inside images. |
/printer | 300 dpi | Check color and fonts for the intended printer. |
/prepress | 300 dpi | Does not guarantee print readiness. |
Downsampling also uses a resolution threshold; it does not resize every image to the target.
These presets alter more than resolution, including color and font handling. /prepress is not a
highest-quality switch. For a specific image requirement, use the
individual image controls
instead of adding more presets.
Choose a compatibility level
The example explicitly requests PDF 1.7 after selecting /ebook. Use an older level only when a
recipient requires it. Do not lower the PDF version just to chase a smaller file: this can disable
compression features, and output below PDF 1.4 can force transparent pages into bitmaps. See the
pdfwrite limitations
before changing this setting.
For a collection of PDFs, evaluate representative documents before automating the conversion. Keep a separate output location for each input and compare results individually; one successful scan tells you little about a vector drawing or a form.
Common errors and solutions
- A command is missing: Install the corresponding package and check that its executable is on
PATH. Poppler supplies both inspection commands used here. File existsfrommkdir: The overwrite guard worked. Inspect the previous trial and rename its directory before starting another. Do not add-pto bypass the guard.- Missing, encrypted, or corrupt input: Check the filename and open the original in a viewer. Obtain a readable, authorized source copy if necessary. Do not use a partial conversion.
- Font substitution or unexpected appearance: Prefer re-exporting the source document with its fonts embedded. A smaller file with changed text is not an acceptable result.
- Permission denied or disk full: Use a writable working directory with room for the new PDF and page previews. Keep the failed trial separate when retrying.
Licensing note
Ghostscript is available under the AGPL and a commercial license. If you plan to distribute it with an application or operate a service, review Artifex’s licensing information for that use.
