Skip to content
TrySitePrice

Web optimize PDF

An ordinary PDF keeps the table of its contents at the end, so a reader has to fetch the whole file before it can draw anything. Linearising rewrites the file so the first page and the information needed to find it come first. Which engine does that matters, and the result names it: qpdf reorders the bytes and leaves the image and font streams exactly as they were, while the Ghostscript fallback reassembles the document, and its own documentation sets SubsetFonts and the image encoding options to true by default. This is the one size-related tool here that has to run on the server.

Linearising reorders the file so a reader can show page one before the rest has arrived. With qpdf nothing else changes; with the Ghostscript fallback the document is rebuilt on the way through.

Rewriting the cross-reference table into a linear layout is not something the browser-side PDF library can do — it has no write path for it — so this is the one size-related tool that has to run on the server.

Questions about web optimisation

01How is this different from compressing?

Compression makes the file smaller by throwing quality away. Linearising aims at the page order instead, so the size barely changes and a reader can show page one without waiting for the download to finish. On the qpdf path that is the whole of it: content streams are not touched. On the Ghostscript path the document is rebuilt on the way through, so it is a reorder plus a re-author, not a pure reorder.

02My file got slightly larger. Is that wrong?

No, it is expected. A linearised file carries a hint table and a first-page section, which costs a little space. The point was never the size; it was the page order.

03Why can this not run in my browser like the other tools?

Because writing a linearised layout means rebuilding the cross-reference table in a specific order, and the browser-side PDF library has no write path for it. Rather than pretend, this one calls qpdf or Ghostscript on the server.

04Which engine ran, and does the difference matter?

The result line names the program. qpdf is tried first and is the one to want: it rewrites the file structure and leaves every content stream alone. Ghostscript is the fallback, and it works by reassembling the page description rather than copying it, with SubsetFonts, EncodeColorImages, EncodeGrayImages and AutoFilterColorImages all defaulting to true in its own pdfwrite documentation. JPEG and JPEG 2000 data can still pass through untouched, because PassThroughJPEGImages and PassThroughJPXImages also default to true, but fonts are re-subset. If byte-identical images and fonts matter to you, install qpdf on the server rather than relying on the fallback.

05Does it matter if the PDF is small?

Hardly at all. Under a megabyte or two, the whole file arrives before a reader could have shown the first page anyway. The gain shows up on long documents served over slow connections.

Related tools