Compress PDF API: Shrink Uploaded PDFs Before You Store Them

If your app accepts PDF uploads, a few users will send phone scans and image-heavy reports that are ten times bigger than they need to be. They fill your storage, slow down every download and bounce off email attachment limits. Compressing each PDF once, right after upload, fixes that for the life of the file. This guide shows how with the PrivConvert file conversion API, with real numbers from our own test files.

What actually makes a PDF big

In almost every oversized PDF the weight is in the pictures. A page scanned at 300 DPI, or a report with full-resolution photos from a camera, carries far more pixels than a screen or an office printer can use. Text, by contrast, takes a few kilobytes per page. So good compression does one thing well: it resamples heavy images to a sensible resolution and leaves everything else alone.

That is what the compress endpoint does:

  • Photos and scans larger than about 150 DPI at their size on the page are scaled down and saved as JPEG at quality 75.
  • Text and vector graphics are never touched, so they stay sharp, selectable and searchable.
  • Images that are already small, black and white fax-style images, print (CMYK) images and transparency masks are kept exactly as they are.
  • Unused objects are removed and the file is repacked and optimised for fast opening on the web.

Before and after

These are real files run through the API, not best-case examples:

FileBeforeAfterSaved
Scanned letter, 6 pages, 300 DPI2.89 MB1.08 MB62%
Photo report, 20 pages9.74 MB4.88 MB50%
Text document, 1 page, no images22 KB18 KB20%

Compression itself took about 0.6 seconds for the scan and 2.6 seconds for the 20-page report. Add the time to upload the original and download the result, which for large files depends mostly on your connection.

One request

curl -X POST https://privconvert.com/api/convert/compress-pdf \
  -H "Authorization: Bearer $PRIVCONVERT_API_KEY" \
  -F "[email protected]" \
  -o scan-small.pdf

There are no settings to tune. Send the PDF in a field called file and the compressed PDF is the response body.

Compress on upload, keep whichever is smaller

The pattern that works in production: compress once when the file arrives, compare the sizes, and store the smaller one. A PDF that is already well optimised can come back about the same size, and a damaged or password protected file is refused, so in both cases you simply keep the original.

Python

from privconvert import PrivConvert, BadRequestError, ConversionFailedError

pc = PrivConvert()  # reads PRIVCONVERT_API_KEY

def shrink(original: bytes, name: str) -> bytes:
    try:
        small = pc.convert("compress-pdf", original, filename=name).content
    except (BadRequestError, ConversionFailedError):
        return original  # password protected or damaged: keep as is
    return small if len(small) < len(original) else original

Node.js

import { PrivConvert, BadRequestError, ConversionFailedError } from "privconvert";

const pc = new PrivConvert(); // reads PRIVCONVERT_API_KEY

export async function shrink(original, name) {
  try {
    const { content } = await pc.convert("compress-pdf", original, { filename: name });
    return content.length < original.length ? content : original;
  } catch (err) {
    if (err instanceof BadRequestError || err instanceof ConversionFailedError) return original;
    throw err;
  }
}

PHP and Laravel

use PrivConvert\PrivConvert;
use PrivConvert\File;
use PrivConvert\Exception\BadRequestException;
use PrivConvert\Exception\ConversionFailedException;

$pc = new PrivConvert();
$original = file_get_contents($request->file('document')->path());

try {
    $small = $pc->convert('compress-pdf', File::fromString($original, 'upload.pdf'))->content;
    $stored = strlen($small) < strlen($original) ? $small : $original;
} catch (BadRequestException | ConversionFailedException $e) {
    $stored = $original;
}

Go

pc, err := privconvert.New() // reads PRIVCONVERT_API_KEY
if err != nil {
    return err
}

res, err := pc.ConvertFile(ctx, "compress-pdf", "scan.pdf", nil)
switch {
case errors.Is(err, privconvert.ErrBadRequest), errors.Is(err, privconvert.ErrConversionFailed):
    // password protected or damaged PDF: store the original
case err != nil:
    return err
default:
    if err := res.Save("scan-small.pdf"); err != nil {
        return err
    }
}

Run it in a background job rather than inside the upload request, so a large scan never makes the user wait. For a backlog of existing files, the batch endpoint takes up to 20 PDFs per request; see converting thousands of files for pacing and retries.

When not to compress

  • Print production files. Files going to a commercial printer need their full-resolution images and colour data. Keep the originals.
  • Signed PDFs. Any change to the file breaks a digital signature. Compress before signing, never after.
  • Archives with legal requirements. If you must keep the exact file a user submitted, store the original and use the compressed copy for previews and downloads.

Privacy

Uploaded PDFs are often contracts, IDs, payslips and medical letters. PrivConvert processes them in memory and returns the result in the same response: nothing is written to disk and nothing is kept. Every PDF we return also has the producing software and the timestamps removed from its metadata, while the title and author you set are kept. For a deeper look at the content of uploaded PDFs, see removing scripts and attachments from PDFs.

Prefer to do it by hand? The free online PDF compressor uses the same engine, and how to reduce PDF file size covers the manual options.

FAQ

How much smaller will a PDF get?

It depends on what is inside. Scans and photo-heavy reports usually shrink by half or more; in our tests a scanned document went from 2.9 MB to 1.1 MB. PDFs that are mostly text are already small and shrink less, typically 10 to 20 percent.

Does compression make text blurry?

No. Text and vector graphics are never rasterised or re-encoded, so they stay sharp at any zoom and remain selectable and searchable. Only large photos and scans are resampled.

What resolution are images reduced to?

Images larger than about 150 DPI at the size they appear on the page are scaled down to that, and saved as JPEG at quality 75. That is sharp on screen and fine for office printing. Images that are already small are left alone.

Can it compress password protected PDFs?

No. A password protected PDF is refused with a 400 error that says so, instead of being returned unchanged. Remove the password first with the unlock tool if you have the right to, then compress.

Are uploaded PDFs stored?

No. The PDF is processed in memory and the smaller file is returned in the same response. Nothing is written to disk and no copy is kept.

Developer resources:
Official SDKs Error reference Plans & free trial Compress PDF online