mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-11-11 00:15:26 +00:00
PDF Optimizer > Discard User Data gets "Discard external cross-references", off by default. It takes GoToR, GoToE, Launch and ImportData actions, and Thread actions with an /F, out of the actions of link annotations and bookmarks, and the actions chained after one through /Next take its place in order. Other annotations, such as form fields, keep their actions. A link left with no action and no /Dest is removed from its page; when the link is an indirect object, that object is rewritten without the action for anything else that refers to it. A bookmark keeps its title, place and children, including a bookmark stored directly inside another one. Links within the document and web links stay. An indirect action that changes is written as a new indirect object, except the first action left at the head of an /A, which is copied into the /A directly when other actions follow it. Objects that use the old action keep it. An /A with more than 100,000 values to check, or one that opens another document and needs more than 100,000 values to rewrite, stops the optimization with OptimizeError::ExternalRefLimit, and so does an outline of more than 100,000 bookmarks. doc_optimize takes discard_external_refs and reports external_links and external_bookmarks.
pdfcraft-optimize
Layer L4: the PDF Optimizer (execution plan M11.1; Acrobat's Reduce File Size and Optimize PDF ▸ Advanced optimization).
let report = optimize(&mut doc, &Settings::default())?; // Reduce File Size's choices
let report = optimize(&mut doc, &Preset::Mobile.settings())?; // smaller, for screens
// Or with progress: called before each image and the clean-up; `false` cancels.
let report = optimize_with_progress(&mut doc, &settings, &mut |stage| keep_going)?;
- Presets: Standard (Reduce File Size's choices: images above 225 ppi to 150 ppi, JPEG medium) and Mobile (above 144 ppi to 96 ppi, JPEG high), as in Acrobat's Settings menu.
- Merge image fragments (
fragments, on by default): some PDF printers draw a page picture as thousands of images one pixel row high. Runs of upright 8-bit grey or RGB images in one colour space, sharing their left and right edges and stacked edge to edge (at least four, drawn with nothing else between them), become one lossless image at their finest resolution, which the image settings then resample; the strips are dropped by Clean Up. A real page printed this way went from 4.3 MB to 231 KB (Standard) and 161 KB (Mobile). Not merged yet: side-by-side tiles, rotated strips, masks. Content arrays use the shared space boundary policy, so trailing comments, split operands and graphics state keep their meaning after merging. - Images: the effective resolution of each image is measured where pages draw it (the CTM through form XObjects, up to 12 deep; the smallest over all uses). If a use can't be measured (forms nested deeper, or drawn too many times), no image is resampled. Colour and grayscale images above a threshold are resampled with a bicubic (Catmull-Rom) filter to a target resolution and stored as JPEG (Acrobat's quality steps) or ZIP; soft masks are resampled with their image. An image is replaced only if the result is smaller. Left alone: CMYK and other colour spaces, image masks, decode arrays, more than 8 bits, JPEG 2000, JBIG2 and CCITT.
- Discard objects: thumbnails, alternate images, document tags, print settings.
- Clean up: Flate for streams with no filter; for streams with an outer ASCII85 or ASCIIHex
layer, that layer is removed when the result is smaller (the inner filters keep their encoded
bytes). Streams with
/DecodeParms, images and object streams are left alone. - Discard external cross-references (off by default):
GoToR,GoToE,LaunchandImportDataactions, andThreadactions with an/F, are taken out of link annotations and bookmarks, and the actions chained after one through/Nexttake its place. Other annotations, such as form fields, keep their actions. A link left with no action and no/Destis removed from its page, and its object, when something else refers to it, keeps the link without the action; a bookmark keeps its title and children. Links within the document and web links (URI) stay. An outline of more than 100,000 bookmarks stops the optimization with an error.
The engine runs Remove Hidden Information (pdfcraft-redact) for the user-data categories,
then this, then merges identical objects and writes a full, compressed save
(pdfcraft_engine::optimizer::OptimizeJob, which the UI runs on a worker thread with a
progress bar). JPEG decoding and
encoding and resampling come from the image crate.