PDF Optimization for Web and Email: Size, Hidden Data and Fast Loading
Optimizing a PDF before you publish or send it is three jobs, not one: make it small enough, make sure it doesn't carry data you didn't mean to share, and serve it so it loads well. I tested each on real files, including a "redacted" offer letter whose salary was still one command away.
The short version: shrink the images (that is where the size is), strip the metadata, make sure the file has no earlier revisions or covered-up text, and serve it with range requests if it is large. A short script can check all of that before a PDF leaves your hands, and there is one at the end of this post.
Everything below was run on November 27, 2026, on macOS 26.7.3 with headless Chrome 158 and pypdf 7.2.0. The test documents are made up.
Size: images are almost the whole story
In my measurements for the PDF compression guide, a 3 page scan at 300 pixels per inch was 9,208,192 bytes. Resizing its images to 150 ppi and re-saving them as JPEG quality 80 brought it to 1,627,176 bytes, with body text I couldn't tell apart at 200% zoom. A lossless pass on the same file saved 172 bytes. A 4 page text-only report printed from Chrome was 25,493 bytes, and Preview's Reduce File Size filter made it 43,078.
So the first question is always "what is in it". This prints every image with its pixel size, so you can work out its resolution on the page:
from pypdf import PdfReader
r = PdfReader("input.pdf")
for n, page in enumerate(r.pages, 1):
w_in = float(page.mediabox.width) / 72 # PDF units are 1/72 inch
for img in page.images:
print(n, img.name, img.image.size, round(img.image.width / w_in), "ppi across the page")
On my scan it printed 1 image.jpg (2550, 3300) 300 ppi across the page for each page, which tells you exactly how much room there is. If everything is already near 150 ppi, compression won't give you much, and you should look at what else is in the file.
For email, the size you need is your mail provider's attachment limit, and it is worth checking rather than guessing, because a gateway in between can be stricter. If the file is still too large after optimizing, sending a link is better than splitting a document into parts.
Hidden data: what a PDF says about you
I printed a two line "offer letter" from an HTML page with headless Chrome, then read its metadata:
{'/Title': 'Offer letter draft',
'/Creator': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) ... HeadlessChrome/158.0.0.0 Safari/537.36',
'/Producer': 'Skia/PDF m158',
'/CreationDate': "D:20261126223304+00'00'", ...}
Chrome copied the page's <title> into the PDF's Title field. If your HTML template's title says "draft", "internal" or a customer's name, so does every PDF made from it. Many document tools also have an Author field, and some fill it in from the account that made the file. Check what yours writes before a PDF goes on a public page.
Stripping it with pypdf:
from pypdf import PdfReader, PdfWriter
w = PdfWriter(clone_from="offer-meta.pdf")
w.metadata = None
w.write("offer-clean.pdf")
print(PdfReader("offer-clean.pdf").metadata) # None
One catch: tools add metadata back. When I rebuilt a page with Pillow later in this post, the new file got a Title equal to its own file name. Strip metadata as the last step, not the first.
Covered text is not removed text
This is the one that does real damage. I drew a black box over the salary line of the offer letter and saved it the way many editors do, as an incremental update. In a viewer, the line is gone. Then I asked pypdf for the text:
text after black box:
'Offer letter\nSalary: 85,000 per year. Internal note: candidate also interviewing elsewhere.'
The box is a shape drawn on top. The text underneath is still there, and anyone can copy it or extract it. The incremental save made it worse. The file now ends with %%EOF twice, because the edit was appended to the original. Cutting the file at the first %%EOF gave me back the untouched original revision:
b = open("offer-boxed.pdf", "rb").read()
open("rev1.pdf", "wb").write(b[:b.find(b"%%EOF") + 5])
# revision 1 recovered, text: 'Offer letter\nSalary: 85,000 per year. ...'
Real redaction removes the content. If your tool has a proper redaction feature that says it removes text, use it and then check with the extraction above. If it doesn't, the dependable fallback is to flatten the page into an image. I rendered the boxed page at 300 ppi and rebuilt the PDF from the image:
flattened: page 612 x 792 pt, text '', 122,243 bytes
after stripping metadata: 122,091 bytes, metadata None
No text, no earlier revision, no metadata. The price is that the page is no longer searchable or selectable, and the file is bigger than the 2 line original. For a sensitive page, that is a fair trade.
Fast loading: linearization only helps if the server allows range requests
A linearized ("fast web view") PDF puts what the first page needs at the start of the file, so a viewer can show page 1 while the rest downloads. To do that, the viewer asks for parts of the file with HTTP range requests. If the server ignores them, linearization buys nothing. I checked two public PDFs:
$ curl -s -r 0-1023 -o /dev/null -w '%{http_code} %{size_download}\n' https://www.rfc-editor.org/rfc/rfc9110.pdf
200 2858365
$ curl -s -r 0-1023 -o /dev/null -w '%{http_code} %{size_download}\n' \
https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf
206 1024
The first server ignored the range and sent all 2.86 MB. The second answered 206 Partial Content with exactly the 1,024 bytes I asked for, and also sent Accept-Ranges: bytes. Neither file had the /Linearized marker in its first kilobyte, which is the quick check:
head -c 1024 file.pdf | grep -a -c /Linearized # 1 = linearized, 0 = not
None of the PDFs I made with Chrome, Preview or pypdf were linearized either. On a machine where you can install it, qpdf --linearize in.pdf out.pdf does it, and the qpdf CLI reference covers the option. I couldn't install qpdf on this Mac, so I have no before and after numbers for it. For PDFs of a few hundred kilobytes, don't bother. It matters for big reports and catalogues.
Two server details while you are there. Serve PDFs with Content-Type: application/pdf, and add X-Content-Type-Options: nosniff, as w3.org does. If the files change under the same URL, keep the cache time short or put a version in the file name.
A pre-publish check you can run in CI
Everything above fits in one script. It fails the build if a PDF is too big, still carries identifying metadata, or has more than one revision:
import sys
from pypdf import PdfReader
MAX_BYTES = 5_000_000
bad = 0
for f in sys.argv[1:]:
raw = open(f, "rb").read()
r = PdfReader(f)
problems = []
if len(raw) > MAX_BYTES:
problems.append(f"{len(raw)} bytes, over {MAX_BYTES}")
meta = {k: v for k, v in (r.metadata or {}).items() if k in ("/Author", "/Title", "/Subject", "/Keywords")}
if meta:
problems.append(f"metadata {meta}")
if raw.count(b"%%EOF") > 1:
problems.append(f"{raw.count(b'%%EOF')} revisions, earlier versions are recoverable")
print(("FAIL " if problems else "ok ") + f + " " + ", ".join(problems))
bad += bool(problems)
sys.exit(1 if bad else 0)
Run against the files from this post:
FAIL offer.pdf metadata {'/Title': 'Offer letter draft'}
FAIL offer-meta.pdf metadata {'/Title': 'Offer letter draft', '/Author': 'j.example'}
FAIL offer-boxed.pdf metadata {'/Title': 'Offer letter draft'}, 2 revisions, earlier versions are recoverable
ok offer-final.pdf
FAIL scan.pdf 9208192 bytes, over 5000000, metadata {'/Title': 'scan'}
$ echo $?
1
It can't see text hidden under a box, because that needs a person to know what should not be there. For that, extract the text and read it, or search it for the words you meant to remove. Set MAX_BYTES to whatever your real limit is.
Where SecureBin fits
SecureBin doesn't have a PDF optimizer. Everything here runs on your machine with Python, Preview or the command line, which is the point for documents like offer letters. Two tools help around the edges. The Image Compressor shrinks images in your browser before they go into a PDF you build. The Hash Generator gives a file's SHA-256 locally, so the person you send it to can confirm they have the same file.
Related reading: compressing PDFs with measured results, merging PDFs locally, converting HTML to PDF.