Expand description
§The Document — pdfcer’s single object model (load side)
Ties the layers together: retained source buffer → header probe →
xref chain → eagerly parsed indirect objects → reference resolution.
Spec sources: iso32000__s__7.5.5.md (load algorithm),
iso32000__s__7.3.10.md (resolution semantics, dangling-ref rule),
iso32000__s__7.7.2.md (catalog) in the PDF-spec RAG. Clause
numbers are ISO 32000-1:2008.
§THE named invariant: one Document, both directions
Document is simultaneously the parse result AND (from the first
editing Pass on) the write source
(docs/decisions/001-oxidize-pdf-adopt-vs-build.md §6.1 item 3). No
separate builder/generation model may ever be introduced — the
audited prior art shows exactly how that bifurcation forecloses
round-trip editing. Write-side machinery (dirty tracking, the
§7.5.6 incremental writer) lands in the first editing Pass and
operates on this type.
§The retained buffer
The document owns the complete source bytes for its lifetime. Every
file-level IndirectObject records a
ByteSpan into this buffer (its full
N G obj … endobj definition), which is what makes
minimal-diff/incremental re-emission mechanical (ARCHITECTURE.md §5;
crate::span). Memory cost is the file size — the deliberate price
of the round-trip invariant, and small next to any renderer’s
working set.
Objects that lived inside an object stream (§7.5.7) have no span
of their own; they carry Provenance::ObjectStream instead,
naming the container and index. See Provenance for why that
distinction is a type and not a sentinel.
§Eager parse, strict failure (Pass 1 posture)
load/from_bytes parse every in-use object up front; any
malformed object fails the whole load with a precise error. Rationale:
fail-clean over partial success (ARCHITECTURE.md §10) — a viewer
that opens a file “successfully” and then errors object-by-object
gives worse diagnostics than one clean refusal naming the offset.
Lazy/tolerant loading (open-what-you-can) is a deliberate later
feature for damaged-file recovery, driven by corpus evidence, not a
default.
§Two phases, in this order (§7.5.7 + §7.5.8.3)
- File-level objects — every type-1 (
InUse) xref entry is parsed at its offset. - Compressed objects — every type-2 (
InStream) entry is resolved through its container.
The order is forced, not stylistic: a container object stream is
itself an ordinary file-level stream object reached through a
type-1 entry, so phase 2 needs phase 1 to have finished. §7.5.7
guarantees this terminates at two levels — an object stream may not
contain a stream, so a container can never live inside another
container, and the object supplying a container’s /Length may not
be compressed either. There is no recursion to guard, only an
ordering to respect.
Each container is decoded once and cached for the duration of
the load (ObjectStream); a container holding 200 objects would
otherwise be inflated 200 times.
§Resolution semantics (§7.3.10, §7.5.4)
Document::get / Document::resolve implement, exactly:
- dangling reference → null (“shall not be considered an error”) — including object numbers with no xref entry at all;
- generation mismatch → null (a stale reference; the xref entry’s generation is part of the identity);
- free entry → null (§7.5.4 free-list mechanics — how deleted and hybrid-hidden objects read as absent);
- reference chains are depth-guarded (
MAX_RESOLVE_DEPTH, pdfcer policy —5 0 obj 5 0 R endobjis legal syntax and must not loop).
Structs§
- Document
- A loaded PDF document: retained bytes + parsed structure.
- Document
Encryption - Load
Options - How to load a document that contradicts itself (
Pass 283.0).
Enums§
- DocError
- Errors from loading a document.
- Encrypted
Refusal - The cause of an editing verb’s encryption refusal (every verb’s
DocumentEncrypted/Encryptederror variant). - Load
Anomaly - One thing the file said that contradicted itself, and what pdfcer decided
about it (
Pass 283.0). - Unreadable
Object Policy - What to do when one indirect object cannot be loaded (
Pass 283.0).
Constants§
- MAX_
RESOLVE_ DEPTH - Maximum reference-chain hops
Document::resolvefollows before declaring a cycle and yielding null.