pub struct Document { /* private fields */ }Expand description
A loaded PDF document.
Implementations§
Source§impl Document
impl Document
Sourcepub fn load(data: Vec<u8>) -> Result<Document>
pub fn load(data: Vec<u8>) -> Result<Document>
Loads a document from bytes: locates the %PDF-x.y header (scanning
the first 1 KiB, defaulting to 1.4), loads the xref, and sets up
decryption for files using the Standard security handler (RC4 or AES)
under the empty user password (password-protected files yield
Error::Encrypted).
The page tree is not walked here: it is flattened lazily on the
first page access, so opening a document (or reading page_count) does
not parse every page dictionary.
Sourcepub fn open(path: impl AsRef<Path>) -> Result<Document>
pub fn open(path: impl AsRef<Path>) -> Result<Document>
Reads the file at path and loads it via Document::load.
Sourcepub fn get(&self, r: ObjRef) -> Result<Object>
pub fn get(&self, r: ObjRef) -> Result<Object>
Fetches an indirect object by reference (xref lookup, object-stream indirection, cached). A generation mismatch between the request and the file is tolerated (lenient).
Sourcepub fn resolve(&self, o: &Object) -> Result<Object>
pub fn resolve(&self, o: &Object) -> Result<Object>
Chases reference chains with a depth guard of
MAX_RESOLVE_DEPTH (beyond that:
Error::CircularReference, naming the last reference followed); a
reference to a missing or unreadable object resolves to Null
(lenient).
The loop is crate::source::resolve_sync_with, shared with the
provided crate::source::ObjectSource::resolve, so the two cannot
drift apart. It reaches Document::get through this type’s
ObjectSource implementation, which forwards to it unchanged.
Sourcepub fn stream_data(&self, s: &Stream) -> Result<Vec<u8>>
pub fn stream_data(&self, s: &Stream) -> Result<Vec<u8>>
Decodes a stream’s data through its filter chain, resolving indirect filter parameters against this document.
Sourcepub fn seed(&self) -> DocumentSeed
pub fn seed(&self) -> DocumentSeed
The document’s shareable core: everything immutable, nothing cached. Forces the page tree first, so no document built from the seed re-walks it.
Sourcepub fn from_seed(seed: DocumentSeed) -> Document
pub fn from_seed(seed: DocumentSeed) -> Document
A handle to the same document for another thread: share a
DocumentSeed across the thread boundary — the seed is Send and
Sync; the document, whose caches are single-threaded by design, is
neither — and materialize one of these per thread.
The immutable core is shared — the file bytes, the merged cross-reference table, the decryption key material, and the flattened page tree — while the interior caches start fresh, private to this document. That is the whole design: per-page work is independent and lock-free precisely because nothing mutable is shared, so N documents on N threads contend on nothing.
map_pages is the ready-made consumer.
Sourcepub fn fork(&self) -> Document
pub fn fork(&self) -> Document
Document::seed and Document::from_seed in one step, for a
fork used on the calling thread.
Sourcepub fn page_count(&self) -> usize
pub fn page_count(&self) -> usize
Number of pages.
Reports the page tree’s declared /Count — the same value mature
engines return — without walking the tree, so it is cheap on an
otherwise-untouched document. If /Count is absent or implausible the
tree is flattened and its true (lenient, cycle- and depth-guarded)
length is returned instead. Once the tree has been flattened for any
reason, that flattened length is authoritative.
Source§impl Document
impl Document
Sourcepub fn elements(&self, opts: ElementOpts) -> Elements<'_> ⓘ
pub fn elements(&self, opts: ElementOpts) -> Elements<'_> ⓘ
Lazy iteration over the document’s elements. Physical elements come
in file order (header, objects by offset with object-stream members
after their container, xref sections newest→oldest, trailer,
startxref, eof); logical elements follow in document order. Nothing
is parsed or decoded before it is yielded; an element that fails to
parse yields Err for that item and iteration continues.
Physical objects sort by (file offset, member index, object number):
in-file objects use their own offset with member index 0;
object-stream members use their container’s offset with member index
1 + index so they directly follow their container in stream-index order;
members whose container is missing or free sort last (offset u64::MAX)
and surface as Err items; ties on offset and member index break by
ascending object number.
Trait Implementations§
Source§impl ObjectSource for Document
Forwards to the inherent methods, so reading a document through the trait
is bit-identical to reading it directly. resolve is forwarded too, even
though the provided implementation is now the same shared chase
(crate::source::resolve_sync_with) that the inherent method delegates
to: routing it through the inherent method keeps the two entry points a
single call, so they cannot drift should Document::resolve ever grow
document-specific behaviour.
impl ObjectSource for Document
Forwards to the inherent methods, so reading a document through the trait
is bit-identical to reading it directly. resolve is forwarded too, even
though the provided implementation is now the same shared chase
(crate::source::resolve_sync_with) that the inherent method delegates
to: routing it through the inherent method keeps the two entry points a
single call, so they cannot drift should Document::resolve ever grow
document-specific behaviour.