By Vijaykrishna Ram and Vinayak Baranwal

Large PDF files slow down uploads, hit email attachment limits, and eat storage on shared servers. On Linux, you have several reliable ways to bring those sizes down — from a single Ghostscript command that takes seconds to run, to GUI tools that need no terminal at all.
The most direct approach is Ghostscript (gs) with the -dPDFSETTINGS=/ebook preset, which handles most PDFs well and works from any distro’s package manager. For a shorter one-liner, ps2pdf wraps the same engine. If the PDF is mostly images, ImageMagick’s convert command lets you control density and quality directly. For lossless cleanup without touching pixels, cpdf -squeeze removes redundant objects and metadata bloat. And if you prefer not to use the terminal at all, PDF Arranger and LibreOffice Draw both export compressed PDFs from the desktop.
These methods work on all major Linux distributions. Readers new to the Linux terminal can start with An Introduction to Linux Basics. For retrieving PDFs from remote hosts before compressing them locally, see Downloading Files on the Linux Command Line.
/ebook: gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile=out.pdf in.pdf.ps2pdf for mixed text and image PDFs; both accept
-dPDFSETTINGS presets for consistent, repeatable output. Use ImageMagick
when the PDF is image-only and you need quick density and JPEG quality
control. Use cpdf -squeeze for lossless structural cleanup with no pixel
resampling. Use GUI export for one-off desktop jobs where scripting adds no
value.ls -lh file.pdf or du -sh file.pdf before and after./ebook is the best general-purpose Ghostscript preset between size and readability.policy.xml before convert reads or writes PDFs.Before picking a compression method, it helps to know what is making your specific PDF large. Run du -sh file.pdf to confirm the file size, then use pdfinfo to get a quick summary of page count, dimensions, and embedded metadata:
pdfinfo input.pdf
Title: Q3 Report
Pages: 42
Page size: 595.28 x 841.89 pts (A4)
File size: 76534291 bytes
Optimized: no
PDF version: 1.6
If the file is not optimized and has many pages at large dimensions, image resampling via Ghostscript is likely the right first step. If the file is small in page count but still large, embedded images or font bloat are the more probable cause. The three root causes below map directly to the methods in this tutorial.
Embedded high-resolution images are the leading cause of oversized PDFs on Linux systems. A single slide deck exported at 300 dpi for print, or a photograph-heavy report, can run to hundreds of megabytes before any compression. To check which images are embedded and at what resolution, install poppler-utils if it is not already present, then run pdfimages:
Debian / Ubuntu / Linux Mint:
sudo apt install poppler-utils
Fedora:
sudo dnf install poppler-utils
Arch Linux:
sudo pacman -S poppler
openSUSE:
sudo zypper install poppler-tools
Then run:
pdfimages -list input.pdf
page num type width height color comp bpc enc interp object ID
-------------------------------------------------------------------
1 0 image 2480 3508 rgb 3 8 jpeg no 6 0
2 1 image 2480 3508 rgb 3 8 jpeg no 8 0
If images are at 300 dpi or higher and the PDF is intended for screen reading, resampling to 150 dpi with Ghostscript’s /ebook preset removes most of the excess without changing how the document reads on screen.
Each scanned page is a rasterized image, not selectable text, until you run OCR. Scans at 300 dpi to 600 dpi are common for archival quality, so a hundred-page document can reach hundreds of megabytes with no text layer for font compression to exploit. Reducing the effective resolution or converting to grayscale when the source is black-and-white text brings file size down significantly. To confirm whether a PDF is scan-heavy rather than image-heavy, run pdfimages -list input.pdf as shown in the Image-Heavy PDFs section above.
PDFs often carry more than their visible content: duplicate font streams from careless merges, unused color profiles, verbose XMP metadata blocks, and leftover object references from incremental edits. None of this is visible to the reader, but it all counts toward file size. If image resampling alone does not reduce the file enough, a structural cleanup pass with cpdf -squeeze or a fresh Ghostscript rewrite removes this hidden overhead. Use pdfinfo to spot unusually high object counts or unoptimized flags as a starting signal.
Run Ghostscript with -dPDFSETTINGS=/ebook (or another preset) to rewrite the PDF with smaller image data and a cleaner internal structure. It is available in the default package repositories for all major Linux distributions, works on mixed text-and-image PDFs, and produces consistent output across runs using the same flags.
Debian / Ubuntu / Linux Mint:
sudo apt install ghostscript
gs --version
10.02.1
Your installed patch version may differ; expect one version line such as 10.xx.x. For the full list of flags and configuration options, see the official Ghostscript documentation.
Fedora:
sudo dnf install ghostscript
Arch Linux:
sudo pacman -S ghostscript
openSUSE:
sudo zypper install ghostscript
gs \
-sDEVICE=pdfwrite \
-dCompatibilityLevel=1.4 \
-dPDFSETTINGS=/ebook \
-dNOPAUSE \
-dQUIET \
-dBATCH \
-sOutputFile=output.pdf \
input.pdf
-sDEVICE=pdfwrite: selects the PDF output device.-dCompatibilityLevel=1.4: targets PDF 1.4 compatibility for broad reader support.-dPDFSETTINGS=/ebook: applies the ebook compression preset (150 dpi, a practical quality/size balance).-dNOPAUSE: disables pause prompts between pages.-dQUIET: suppresses informational output.-dBATCH: exits Ghostscript after processing completes.-sOutputFile=output.pdf: specifies the output file path.input.pdf: the source file to compress.Note: Never point -sOutputFile at the same path as input.pdf. Ghostscript may produce a zero-byte or corrupted file if the input is overwritten during writing.
| Preset | DPI | Typical Size Reduction | Use Case |
|---|---|---|---|
| /screen | 72 | 70-85% | Web display, email preview, smallest size |
| /ebook | 150 | 40-60% | General sharing, email attachments, e-readers |
| /printer | 300 | 10-30% | Desktop printing, internal documents |
| /prepress | 300 | 5-15% | Commercial printing, color-accurate output |
| /default | varies | Minimal | General-purpose; may increase file size |
Note: /default can increase file size on PDFs that were already
optimized because it does not force aggressive downsampling. If any preset
produces an output file larger than the input, the source PDF is already
well-optimized for images. In that case, run cpdf -squeeze input.pdf -o output.pdf instead for a lossless structural pass that removes object
overhead without altering image quality.
ls -lh input.pdf
-rw-r--r-- 1 user user 73M Jan 10 09:00 input.pdf
Run compression:
gs \
-sDEVICE=pdfwrite \
-dCompatibilityLevel=1.4 \
-dPDFSETTINGS=/ebook \
-dNOPAUSE \
-dQUIET \
-dBATCH \
-sOutputFile=output.pdf \
input.pdf
ls -lh output.pdf
-rw-r--r-- 1 user user 14M Jan 10 09:01 output.pdf
In this example a 73 MB source PDF becomes 14 MB with /ebook, an 81% reduction.
After compression, open the output file and verify that text is readable and images are acceptable at normal zoom. For scanned documents, check that page edges and fine print are not visibly degraded. If quality is insufficient, rerun the command with /printer instead of /ebook and compare the result.
ps2pdf is the right choice when you want the same Ghostscript compression as Method 1 but prefer a shorter command. It ships as part of the ghostscript package, so no separate install is needed. For standard office documents and export-generated PDFs, it produces identical output to the full gs invocation with the same preset.
pdf2ps converts a PDF into a PostScript file, then ps2pdf passes that stream through Ghostscript’s PDF writer to produce a compressed PDF. Most workflows call ps2pdf directly with a -dPDFSETTINGS flag, which skips the intermediate file and runs the same engine in one step.
Single-step form:
ps2pdf -dPDFSETTINGS=/ebook input.pdf output.pdf
ls -lh output.pdf
-rw-r--r-- 1 user user 14M Jan 10 09:05 output.pdf
The output size matches the Ghostscript result from Method 1 for the same input file because ps2pdf uses the same underlying engine. The difference is convenience: fewer flags to type for straightforward jobs. For fine-grained control over color profiles, font embedding, or image filters, use the full gs command from Method 1 instead.
gs command line.Use ImageMagick to rasterize pages at a chosen dpi and JPEG quality when the PDF is already image-like or when you standardize on convert in existing automation.
# Debian / Ubuntu / Linux Mint
sudo apt install imagemagick
convert --version
Version: ImageMagick 6.9.11-60 Q16 x86_64
Warning: On many Debian and Ubuntu systems, ImageMagick’s default security policy blocks PDF read and write operations. Before running the convert command, edit /etc/ImageMagick-6/policy.xml (or /etc/ImageMagick-7/policy.xml) and change the PDF policy line from rights="none" to rights="read|write".
Note: ImageMagick 7 renamed the convert binary to magick. If convert
returns a command not found error, run magick convert instead. To check
which version is installed, run magick --version or convert --version.
<!-- Change this line: -->
<policy domain="coder" rights="none" pattern="PDF" />
<!-- To this: -->
<policy domain="coder" rights="read|write" pattern="PDF" />
convert -density 150 input.pdf -quality 85 output.pdf
-density 150: treats the input as 150 dpi when rasterizing page content.-quality 85: JPEG quality for compressed image data inside the PDF; lower numbers mean smaller files and more visible artifacts.Stronger size cut, with more quality loss:
convert -density 100 input.pdf -quality 60 output.pdf
ls -lh output.pdf
-rw-r--r-- 1 user user 11M Jan 10 09:12 output.pdf
For installation options, policy details, and broader examples, read How To Resize Images with ImageMagick on Ubuntu.
ImageMagick fits image-only PDFs where you already think in terms of dpi and JPEG quality, and you want a short convert line without reading Ghostscript dictionaries. Ghostscript gives preset-based control and tends to preserve text vector paths in mixed documents more predictably. For scripted server work or repeatable command-line pipelines, Ghostscript is the safer default.
Use cpdf when you need to reduce file size without any change to image quality. It targets structural overhead — redundant objects, duplicate streams, and metadata bloat — rather than resampling page content.
cpdf is commercial software with a free community edition distributed as a static binary from Coherent Graphics. Download the current Linux build from the official GitHub binaries tree, then place it on your PATH.
wget https://github.com/coherentgraphics/cpdf-binaries/raw/master/Linux-Intel-64bit/cpdf
chmod +x cpdf
sudo mv cpdf /usr/local/bin/
cpdf -version
cpdf Version 2.7
Note: Confirm the latest binary URL on the official cpdf GitHub repository before scripting downloads, paths can move between releases.
cpdf -squeeze input.pdf -o output.pdf
-squeeze removes redundant objects, unused resources, and duplicate data streams. It does not resample or alter page images, so the visual output is identical to the source. If the file still looks the same but is smaller, the savings came from structural overhead rather than image data.
Note: Size reduction from -squeeze varies significantly by file. PDFs
bloated by merged duplicates, unused font streams, or verbose metadata may
shrink by 30–50%. Image-heavy scans with no structural redundancy will see
minimal change; use Ghostscript with a -dPDFSETTINGS preset for those files.
-squeeze stays lossless relative to source imagery.For users who prefer a graphical interface, two tools cover most compression workflows without requiring terminal access.
Debian / Ubuntu / Linux Mint:
sudo apt install pdfarranger
Fedora, Arch Linux, openSUSE, and other distros (via Flatpak):
flatpak install flathub com.github.jeromerobert.pdfarranger
flatpak run com.github.jeromerobert.pdfarranger
Note: Flatpak must be installed and the Flathub remote must be configured before running the install command. On Fedora, run sudo dnf install flatpak first. On Arch Linux, install flatpak via sudo pacman -S flatpak.
PDF Arranger is a GTK tool for merging, splitting, rotating, and reordering pages. File → Export writes a fresh PDF that often drops duplicated resources from careless merges. Remove blank or redundant pages before export, then pass the result through Ghostscript if you still need smaller images.
Open the PDF in LibreOffice Draw, then follow these steps:
Check the output size with ls -lh output.pdf after export. For a typical office-originated PDF, this approach reduces file size by 30–60% without visible degradation at normal reading zoom levels. This method works best for PDFs that originated as office documents with vector text and charts. For pure scanned pages, Ghostscript with /ebook produces better results.
For most scanned PDFs, this Ghostscript command is the right starting point:
gs \
-sDEVICE=pdfwrite \
-dCompatibilityLevel=1.4 \
-dPDFSETTINGS=/ebook \
-dNOPAUSE \
-dQUIET \
-dBATCH \
-sOutputFile=output_scanned.pdf \
scanned_input.pdf
The /ebook preset targets 150 dpi, which is sufficient for screen reading and typical office review. If you need to understand why scanned PDFs require a different approach than text-based PDFs, or how to push compression further with DPI and color depth controls, the subsections below cover both.
Each page is a full-page bitmap at scanner resolution, often 300 dpi to 600 dpi, so file size scales with pixel count and color channels. Without OCR there is no underlying text for font compression to exploit, which is why scanned books blow past text-export PDFs.
The /ebook command shown at the top of this section targets 150 dpi, which is the right starting point for most scans intended for screen reading. Use /screen instead when the smallest possible file size is the priority and some readability loss is acceptable. To go further with explicit DPI control or grayscale conversion, see the subsection below.
Exact dpi control:
gs \
-sDEVICE=pdfwrite \
-dCompatibilityLevel=1.4 \
-r150 \
-dNOPAUSE \
-dQUIET \
-dBATCH \
-sOutputFile=output_150dpi.pdf \
scanned_input.pdf
-r150 fixes the raster resolution at 150 dpi regardless of other PDFSETTINGS interactions, useful when you standardize archive submissions.
Monochrome or gray text scans:
gs \
-sDEVICE=pdfwrite \
-dCompatibilityLevel=1.4 \
-dPDFSETTINGS=/ebook \
-sColorConversionStrategy=Gray \
-dProcessColorModel=/DeviceGray \
-dNOPAUSE \
-dQUIET \
-dBATCH \
-sOutputFile=output_gray.pdf \
scanned_input.pdf
Color overhead disappears on pages that are visually black ink on white. For monochrome text scans, grayscale output typically removes another 30-50% of bytes compared with keeping a color container.
Batch compression jobs start by gathering files. For shell structure, quoting, and execution bits, see How To Write a Bash Script. See How To Use Find and Locate to Search for Files on Linux to locate PDFs recursively before you run the loop.
#!/bin/bash
# batch_compress_pdf.sh
# Compresses all PDF files in the current directory using Ghostscript.
# Usage: ./batch_compress_pdf.sh [output_directory]
PRESET="/ebook"
OUTPUT_DIR="${1:-./compressed}"
mkdir -p "$OUTPUT_DIR"
for input_file in ./*.pdf; do
[ -f "$input_file" ] || continue
filename="$(basename "$input_file")"
output_file="$OUTPUT_DIR/$filename"
original_size=$(du -sh "$input_file" | cut -f1)
gs \
-sDEVICE=pdfwrite \
-dCompatibilityLevel=1.4 \
-dPDFSETTINGS="$PRESET" \
-dNOPAUSE \
-dQUIET \
-dBATCH \
-sOutputFile="$output_file" \
"$input_file"
compressed_size=$(du -sh "$output_file" | cut -f1)
echo "Compressed: $filename | Before: $original_size | After: $compressed_size"
done
echo "Done. Compressed files saved to $OUTPUT_DIR"
Make the script executable:
chmod +x batch_compress_pdf.sh
Run from the directory that contains the PDFs:
./batch_compress_pdf.sh
Example log lines:
Compressed: report.pdf | Before: 45M | After: 9.2M
Compressed: invoice.pdf | Before: 12M | After: 2.1M
Compressed: manual.pdf | Before: 73M | After: 14M
Done. Compressed files saved to ./compressed
Custom output directory:
./batch_compress_pdf.sh /home/user/pdf_output
Use How To Use Find and Locate to Search for Files on Linux when PDFs live in subdirectories and you need a list before copying them into one folder for this script.
Match the tool to whether the PDF is text-heavy, scan-heavy, or bloated by objects, then pick CLI versus GUI based on where you run the job.
| Tool | Method Type | Compression Control | Interface | Package Availability | Best For |
|---|---|---|---|---|---|
| Ghostscript | Lossy | High (5 presets + flags) | CLI | All major distros (apt, dnf, pacman, zypper) | General compression, scripting, scanned PDFs |
| ps2pdf | Lossy | Medium (inherits gs presets) | CLI | Included with Ghostscript | Quick compression, simple one-liners |
| ImageMagick | Lossy | Medium (density, quality flags) | CLI | All major distros | Image-only PDFs, existing ImageMagick workflows |
| cpdf | Lossless | Low (squeeze only) | CLI | Manual binary install | Lossless deduplication, metadata cleanup |
| PDF Arranger | Lossy | Low (export settings) | GUI | apt / Flatpak (all distros) | Desktop one-off compression, page management |
| LibreOffice | Lossy | Medium (export dialog) | GUI | All major distros | Office-originated PDFs, desktop workflows |
Q: What is the best command to reduce PDF file size in Linux?
A: The most reliable method is Ghostscript with the -dPDFSETTINGS=/ebook preset, which balances quality and file size for most use cases. Run:
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf
Q: Does compressing a PDF in Linux reduce its quality?
A: It depends on the preset. The /screen preset reduces image quality noticeably (72 dpi), while /ebook and /printer preserve readable quality. PDFs containing only text typically show no visible quality change after compression.
Q: How do I compress a PDF in Linux Mint specifically?
A: Ghostscript is available in Linux Mint’s default repositories. Install it with sudo apt install ghostscript and use the standard gs command. PDF Arranger is also available via the Software Manager for GUI-based compression.
Q: Can I compress multiple PDF files at once in Linux?
A: Yes. Use a bash for loop or the batch script in the “How to Batch Compress Multiple PDF Files in Linux” section above. The script processes all .pdf files in the current directory and logs before/after sizes to stdout.
Q: How do I reduce the file size of a scanned PDF in Linux?
A: Scanned PDFs are image-heavy. Use Ghostscript with -dPDFSETTINGS=/ebook or the -r150 flag to reduce the output DPI from the original scan resolution. For grayscale text scans, add -sColorConversionStrategy=Gray to reduce file size further.
Q: Is ImageMagick a good alternative to Ghostscript for PDF compression?
A: ImageMagick works for image-only PDFs but provides less control over compression settings than Ghostscript. On Debian and Ubuntu, you must also update the ImageMagick PDF security policy before the convert command will accept PDF input.
Q: How do I check PDF file size before and after compression in Linux?
A: Use ls -lh filename.pdf for a quick human-readable size, or du -sh filename.pdf for disk usage. Run both commands before and after compression to confirm the reduction.
Q: Does Ghostscript work on all major Linux distributions?
A: Yes. Ghostscript is in the default repositories for Debian, Ubuntu, Fedora, Arch Linux, openSUSE, and Linux Mint. The install command varies:
apt install ghostscript, dnf install ghostscript, pacman -S ghostscript, or zypper install ghostscript.
Start with pdfinfo and pdfimages -list to understand what is driving the
file size, then match the tool to the problem. Ghostscript with /ebook
covers most cases. cpdf -squeeze handles the rest without touching image
quality. For everything else, the tool comparison table points to the right
method.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Building future-ready infrastructure with Linux, Cloud, and DevOps. Full Stack Developer & System Administrator. Technical Writer @ DigitalOcean | GitHub Contributor | Passionate about Docker, PostgreSQL, and Open Source | Exploring NLP & AI-TensorFlow | Nailed over 50+ deployments across production environments.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.