Compress a signed PDF: why the signature breaks and what to do

A compressed copy of a signed PDF fails signature validation. We tested it, and show why the API refuses signed files with signed_pdf and what to do instead.

A signed PDF page with a green SIGNED stamp, and an arrow to a smaller compressed copy with a red INVALID stamp

A compressed copy of a signed PDF is not a signed PDF. We signed a test file, compressed it with the guard turned off, and a validator reported the signature as invalid. So our compress call refuses signed files with 422 signed_pdf and charges nothing, unless you send allow_signature_break: true.

Signed contracts, signed invoices and signed forms are often big, because they carry scanned pages or photos. People want them small enough to email. This guide explains why compression and a valid signature do not mix, what the API does when it meets a signed file, and the order of steps that keeps both the small size and the signature.

Why compression breaks a signature

A digital signature in a PDF is a digest of the file's bytes. The signature dictionary has a ByteRange entry that lists which bytes were digested. To check the signature, a reader computes the digest again and compares it with the stored one. The PDF standard says that differences in the digest values indicate that modifications have been made since the document was signed. The passage is in section 12.8.1 of ISO 32000-1, in the free copy that Adobe hosts.

Compression rewrites the file. It resamples images, repacks objects into streams and writes a new cross-reference table. The bytes the signature covered are not the bytes that come out, so the digest no longer matches. Making the file smaller and keeping the signature valid are two goals that exclude each other.

What the API does with a signed file

POST /v1/pdf/compress checks the document before it touches any image. If the file has signatures, the call stops with a 422 and the code signed_pdf. The error is in the processing_error family, so the file is the problem, not your request. Nothing is stored and you are not charged. The signed_pdf error page lists the same facts.

The message the worker returns is:

{
  "error": {
    "code": "signed_pdf",
    "message": "This PDF is digitally signed; compressing it invalidates the signature. Send options.allow_signature_break: true to proceed."
  }
}

To compress the file anyway, say so in the request:

curl https://api.justapi.tech/v1/pdf/compress \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: 6c1e9a52-0d3b-4f7a-8e21-b5f4a7d90c13" \
  -d '{
    "source": { "url": "https://files.example.com/signed-contract.pdf" },
    "options": { "profile": "email", "allow_signature_break": true }
  }'

The option is off by default on purpose. A pipeline that compresses every file it receives should not break signatures silently.

What we measured

We built a PDF with one photo, signed it with a self-signed certificate, and checked it with pyHanko, an open-source library that signs and validates PDFs. Then we ran the compression engine from the command line with the email profile. The engine is the same script the PDF worker runs, with the pikepdf version pinned in our container. We did not call the live API for this article.

Step Result
Signed input 363,582 bytes. pyHanko: INTACT:TRUSTED,UNTOUCHED
Compress without the option Error signed_pdf, exit code 2, no output file
Compress with allow_signature_break: true 61,294 bytes, 17% of the input
Signed output, checked again One signature found. intact: False. pyHanko summary: INVALID

Two things follow. The guard works as described, and the option does what its name says. The compressed file is much smaller, and its signature is gone as a proof. The signature data is still inside the file, so a PDF reader that checks signatures will show a warning, not a clean mark.

What the guard checks, and what it does not

The check reads one flag. The form dictionary of a PDF can carry SigFlags, and bit 1 of that flag, SignaturesExist, tells readers that the document has signature fields. Signing software sets it. The API refuses the file when that bit is set.

This has two edges you should know:

  • A blank signature box is refused too. The standard defines the flag as "the document contains at least one signature field", not "at least one signature that was filled in". We added an empty signature field to a test PDF, and the engine refused it with the same signed_pdf message. If you know nobody signed the file, allow_signature_break: true is harmless, because there is no signature to break.
  • It is not a validator. The guard does not check that a signature is valid, and it cannot see a signature in a file whose writer left the flag unset. If a legal decision depends on the signature, validate it with a tool built for that.

The order that keeps both

  1. Compress first, sign last. If you create the document, optimise the images and compress the PDF before the signer sees it. The signature then covers the small file, and nobody needs to touch it again.
  2. Keep the signed original as the record. When a signed file arrives from someone else, store it as it came. It is the version that proves what was agreed.
  3. Make the small copy explicitly unsigned. For an email or a preview, compress a copy with allow_signature_break: true, and name it so nobody mistakes it for the signed file, for example contract-0042-preview-unsigned.pdf.
  4. Decide per file, not per pipeline. Do not set allow_signature_break: true as a default in your integration. Set it after a rule or a person decides that this copy does not need to be valid.

If you can analyse a file before you pay for a run, the analyze guide shows how to see what is inside it first.

Two sibling guards

The same pattern applies to two other kinds of file. Each stops with a 422 and charges nothing.

  • PDF/A and PDF/X. The engine looks for the PDF/A or PDF/X marker in the file's XMP metadata. Compression does not preserve those conformance levels, so the call stops with conformance_break. The switch is allow_conformance_break: true. The guard reads the marker, it does not validate conformance.
  • Password-protected files. A file that needs a password to open stops with encrypted_pdf. Send the password in options.password. In our test, the compressed output of a password-protected file was not encrypted, and the output of a file with only permission restrictions had lost them. If you need a protected copy, apply the protection again after the download.

Limits of this test

We tested one synthetic file, one self-signed certificate, one validator and one profile. A signature from a qualified provider, a file with several signatures or a certified document may behave in other ways. The guard is built to refuse any file that carries the flag before it does any work. Test your own files before you rely on a workflow, and check the PDF API reference for the current option list.