Pagecraft

compress

Compress PDF

Make a document smaller, on this device.

Reads
a PDF — application/pdf
Writes
one file — PDFs
Tool page
/compress-pdf

Example

Get an illustrated report under an attachment limit.

Given

  • illustrated.pdf — a PDF with a photograph in it

From code

            import { loadDocument, runOperation } from '@pagecraft/core';

const illustrated = await loadDocument({ bytes, name: 'illustrated.pdf' });

const [output] = await runOperation('compress', [illustrated], {
  mode: 'quality',
  imageQuality: 60,
});
          

From a shell

            pagecraft compress illustrated.pdf --mode quality --image-quality 60
          

What comes back

One PDF of 1 page.

This example is run by the test suite. If it stopped being true, the build would fail.

Options

mode--mode

string, one of

Aim for

Default: 'quality'

  • quality — You choose how much detail to keep.
  • target-size — Pagecraft finds the quality that fits, if one does.

imageQuality--image-quality

integer (%)

Image quality

Lower is smaller and blurrier. It applies to photographs and scans only — text and drawings are never re-encoded.

Only when mode is 'quality'.

Default: 75

targetSize--target-size

number (MB)

Size to get under

The limit you have been given, in megabytes.

Only when mode is 'target-size'.

Default: 5

images--images

boolean

Re-encode pictures

Turn this off to tidy the document’s structure without touching a single pixel.

Default: true

What it cannot do

  • The savings are in the pictures. A document of text and vector drawings is made smaller by tidying its structure, which is worth a few per cent, not a few times.
  • A picture is only replaced when the new one is genuinely smaller, so a diagram or a screenshot is left alone rather than turned to mush.
  • Pictures stored with a palette, at one bit, with transparency, or as JPEG 2000, JBIG2 or CCITT fax are left as they are. The report says which, and why.
  • A target size is aimed at, not promised. A document with nothing to give cannot reach one, and the report says whether it did.
  • Embedded fonts are not subset yet, so a file whose bulk is a font will not shrink much.
  • Re-encoding a picture drops its colour profile, so colours can shift very slightly on a colour-managed screen.