How PDF Compression Works (and How to Do It on Ubuntu)
Published · Updated · By CatalystCodeYou finish a document, export it as a PDF, and try to upload it. Then the website tells you the file is too large. You only have a few pages, so where did all those megabytes come from?
Usually, the answer is inside the pages: a high-resolution scan, a photograph, an embedded font, or content saved with inefficient settings. To make the PDF smaller, you need to know which of those things you can compress without losing something you care about.
PDF compression, in plain language: it reduces the data needed to store a document. Lossless compression stores information more efficiently without discarding it. Lossy compression gives up some detail, often in images, to save more space. Start with lossless compression; reduce image resolution only when you need to and the result is still readable.
We will walk through those ideas first, then use an offline tool on Ubuntu to put them into practice. You do not need to understand PDF internals or write code to follow the compression examples.
Why is my PDF file so large?
Think of a PDF as a container for everything needed to display its pages. That can include text, the fonts used to draw it, photographs, and instructions for lines and shapes. Two PDFs with the same number of pages can have very different file sizes because they contain different kinds of data.
- Scanned pages: a scanner usually stores a picture of each page. Even a page that looks like plain text may really be one large image.
- Photographs and screenshots: a full-resolution image may still be embedded even when it appears small on the page.
- Fonts and other embedded content: fonts, attachments, and additional objects can increase the amount of data the file carries.
- Export settings: some applications prioritize print quality or editing flexibility over a small download.
As a quick clue, try selecting a sentence in your PDF reader. Selectable text suggests a text layer is present. If you cannot select anything, the page may be an image. This is not a definitive test: scanned PDFs can also have an OCR text layer placed over the image.
How does PDF compression work?
1. Lossless compression: store the same information more efficiently
Imagine packing a suitcase more carefully without leaving anything behind. Lossless compression follows a similar idea: it changes how information is stored so the original information can be recovered. In a PDF, that can mean compressing the streams that hold drawing instructions or grouping small internal objects so they compress together.
This is a useful first attempt for text reports, diagrams, and files whose images you want to preserve. It does not mean the output file has identical bytes: the PDF can be reorganized while retaining its content. That distinction matters for digitally signed documents, because rewriting the file can invalidate its signature.
2. Image downsampling: keep fewer pixels
A scan intended for high-quality printing may contain more pixels than you need to read it on a screen. Downsampling reduces those pixel dimensions. For example, reducing an image from 2,000 × 2,000 pixels to 1,000 × 1,000 leaves one quarter of the pixel count. It does not guarantee a PDF that is one quarter the size: image encoding and the rest of the document also matter.
The tradeoff is detail. Small lettering, fine lines, and handwriting may become harder to read. Once those pixels have been removed, increasing the resolution again cannot bring them back.
3. Image encoding: use fewer bytes to describe an image
Resolution and compression quality are different controls. An image can keep the same pixel dimensions but use a lossy encoding such as JPEG to store less detail. This often suits photographs better than small text or sharp line drawings, where compression artifacts can be easier to notice. Different PDF compressors choose different encodings; a quality setting is not the same thing as a DPI setting.
For the engine details behind our examples, see the QPDF compression options and Ghostscript PDF writer documentation.
Lossless vs. lossy PDF compression: which should you use?
| Your PDF | Start with | Check afterward |
|---|---|---|
| Text report or vector diagram | Lossless optimization | Text, layout, and file size |
| Scanned pages or photographs | Lossless, then image downsampling if needed | Small text and image detail |
| Forms or annotations | Lossless optimization | Fields, links, and comments still work |
| Digitally signed original | Keep the signed file unchanged | Any separate compressed copy is not the signed original |
If your PDF hardly shrinks, that does not necessarily mean the compressor failed. Its streams and images may already be efficiently compressed. There is no universal setting that makes every PDF dramatically smaller while preserving everything.
Can you compress a PDF offline?
Yes. A local PDF compressor reads the file on your computer and writes the result there. After installation, you can use it without an internet connection. That is useful when you have limited connectivity or simply do not want to upload a document to another service.
A website can also process PDFs locally in your browser, so being a website does not automatically mean it uploads files. The important question is where the processing happens. For the rest of this guide, we will use a command-line tool that runs the compression engines on your Ubuntu machine.
Meet PDFGraph: our offline PDF compressor for Ubuntu
We built PDFGraph to make this workflow available through a short command. It is a free, open-source tool written in Rust. QPDF handles the lossless pass, and Ghostscript handles optional image downsampling. The goal is straightforward: choose an input, choose how much image detail to keep, and save a separate result.
PDFGraph processes one PDF per command. It has no upload step, account requirement, telemetry, or API key. Once the application and its engines are installed, compression works without an internet connection.
- Lossless: optimize PDF objects and streams without downsampling images.
- Balanced: also try image downsampling at 150 DPI for color and grayscale images.
- Small: also try a lower 72 DPI target for color and grayscale images.
Image modes use twice the selected DPI for monochrome images. These are downsampling targets, not a promise that every image will be rewritten: engine thresholds can leave some images unchanged. Lower-resolution images are not enlarged.
PDFGraph currently focuses on compression. Merging, splitting, OCR, and format conversion are possible directions for future development; they are not commands in this version.
Install PDFGraph on Ubuntu
To build from source, first install Rust 1.74 or later with Cargo using the official Rust installation instructions. Then install the native build tools and compression engines:
sudo apt update
sudo apt install build-essential git qpdf ghostscript
git clone https://github.com/catalystcode-dev/PDFGraph.git
cd PDFGraph
cargo build --release --locked --offline
sudo install -m 755 target/release/pdfgraph /usr/local/bin/pdfgraph
pdfgraph --version
pdfgraph --helpQPDF is required for all modes. Ghostscript is needed for balanced and small. You can omit Ghostscript if you only want lossless compression.
Downloading the source, Rust toolchain, or system packages may require internet access. The --offline build works once the toolchain and build dependencies are installed because PDFGraph has no external Rust crate dependencies. It does not make the initial installation offline.
How to compress a PDF on Ubuntu without reducing image quality
For a text document, a vector diagram, or a PDF where image quality matters, begin with the default mode:
pdfgraph report.pdfThis creates report.compressed.pdf beside the original. PDFGraph leaves report.pdf untouched. To choose a different output location, use -o:
pdfgraph "Monthly Report.pdf" -o "Monthly Report-smaller.pdf"The lossless pass uses QPDF to generate compressed object streams and recompress eligible streams. It does not lower image resolution or intentionally apply lossy image encoding. The QPDF command-line documentationexplains the underlying stream and object options.
How to reduce the size of a scanned PDF
If the lossless result is still too large, try the balanced mode. Its 150 DPI target is a useful starting point for screen reading, but check the smallest text and any detailed images:
pdfgraph scanned-report.pdf --mode balanced -o scanned-report-balanced.pdfFor a smaller candidate, try the 72 DPI mode and compare it with the balanced result:
pdfgraph scanned-report.pdf --mode small -o scanned-report-small.pdfAlways generate each candidate from the original. Repeatedly processing an already downsampled copy cannot restore detail lost in an earlier pass. For more control, choose a DPI value:
pdfgraph scanned-report.pdf --mode balanced --dpi 200 -o scanned-report-200dpi.pdfThe allowed range is 36 to 1200, and --dpi requires an image mode. Higher values retain more image detail but can leave a larger file. Image compression does not intentionally turn every page into a bitmap; Ghostscript generally retains text and vector content where supported by its PDF writer.
How PDFGraph chooses and protects the output
PDFGraph checks the input, creates compression candidates in a private temporary directory, validates their structure with QPDF, and checks that their page counts match. It compares the file sizes and saves the smallest candidate. If compression makes no improvement, it copies the original bytes to the output instead. A successful run can therefore report zero percent saved.
Existing output files are protected unless you explicitly use --force:
pdfgraph report.pdf -o smaller.pdf --forceEven with that flag, the input and output must be different files. The output directory must already exist. The final report shows the before-and-after sizes, the percentage saved, and whether the selected result came from lossless optimization, image compression, or the original file.
What to check before sharing the result
Structural validation is useful, but it cannot prove that every visual detail or interactive feature survived. The Ghostscript PDF output documentation explains that its PDF writer creates a new document from interpreted content. Some information that does not draw marks on a page may not carry across.
- Forms and annotations: image modes can change or discard interactive content. Prefer lossless mode when those features matter, and test the result.
- Digital signatures: a rewritten PDF invalidates existing signatures. Keep signed originals.
- Encryption: PDFGraph rejects encrypted PDFs, including ones that open without asking for a password.
- Archival requirements: PDF/A and PDF/UA conformance are not guaranteed.
- Size limits: the tool does not guarantee an exact target such as 1 MB. Review the output size and readability together.
If a command reports a missing engine, install qpdf or ghostscript through Ubuntu's package manager. If it reports that the output already exists, choose a new filename or deliberately replace it with --force.
Build an installable .deb package
The repository also includes a native Ubuntu packaging script. From the cloned project on Ubuntu, with Rust already installed, run:
sudo apt install build-essential dpkg-dev python3 qpdf ghostscript
bash scripts/build-deb.shFor version 0.1.0 on an amd64 Ubuntu machine, the script produces dist/pdfgraph_0.1.0_amd64.deb. Install the local package with:
sudo apt install ./dist/pdfgraph_0.1.0_amd64.debThe package name depends on the project version and the build machine's architecture. QPDF and Ghostscript remain separately installed system packages. Build inside Ubuntu if you are working from a Mac: an ordinary macOS Rust build produces a macOS executable. These steps build a local package; they do not require PDFGraph to be in Ubuntu's official archive.
Common questions about PDF compression
Can I compress a PDF without losing quality?
Lossless compression can reduce file size without discarding image or document detail, provided there is room to optimize how the content is stored. In PDFGraph, start with the default lossless mode. If the file is already optimized, meaningful further savings may require an image-quality tradeoff.
How do I compress a PDF below 1 MB or 500 KB?
Try lossless mode first, then compare image modes if your document contains scans or photographs. PDFGraph does not offer an exact target-size option. Whether you can meet an upload limit depends on the document. If readable output is still too large, consider re-exporting from the source with smaller images or asking the recipient for another way to send it.
Why does my compressed PDF look blurry?
The images may have been downsampled too far or encoded with too much loss. Return to the original PDF and try a higher DPI, such as 200 instead of 72, or use lossless mode. Compressing the blurry copy again will not recover the missing detail.
Will putting a PDF in a ZIP file make it much smaller?
Sometimes it helps, but many PDFs already contain compressed streams and images, leaving little for ZIP to reduce. A ZIP archive also will not satisfy an upload form that accepts only PDF files. PDF compression produces another PDF that you can open directly in a PDF reader.
Is PDFGraph free, and does it work offline?
Yes. The Rust wrapper is MIT licensed and has no account or subscription requirement. Compression runs locally after setup. Installing the toolchain and QPDF or Ghostscript may initially require a download; those engines are separate packages with their own licenses.
Try it on a copy of your PDF
Start with pdfgraph report.pdf, inspect the result, and only try image downsampling if you need a smaller candidate. For a scan, compare balanced mode against the original at the zoom level you expect readers to use.
You can read the implementation, report a problem, or follow development in the PDFGraph GitHub repository. Its integration tests demonstrate checks for text and image PDFs, input preservation, page counts, and identical lossless rendering on generated fixtures. Those checks help catch regressions without claiming that every possible PDF behaves the same way.
If you want to understand the Rust behind the CLI, continue with our Rust tutorials. PDFGraph is a compact example of using Rust to coordinate established document-processing tools around a practical offline workflow.