pub struct Document { /* private fields */ }Expand description
A parsed PDF file.
Opening is cheap: only the header, the cross-reference sections and the
trailer are read. Pages, streams and content are parsed on demand by
Document::get_object, which caches what it reads.
Implementations§
Source§impl Document
impl Document
Sourcepub fn extract_text(&self, page: &Page) -> Result<String>
pub fn extract_text(&self, page: &Page) -> Result<String>
Extract the text of a page’s content streams.
Sourcepub fn extract_text_from(
&self,
content: &[u8],
resources: Option<&Dictionary>,
) -> Result<String>
pub fn extract_text_from( &self, content: &[u8], resources: Option<&Dictionary>, ) -> Result<String>
Extract text from a decoded content stream and its resource dictionary.
Sourcepub fn spans(&self, page: &Page) -> Result<Vec<TextSpan>>
pub fn spans(&self, page: &Page) -> Result<Vec<TextSpan>>
The page’s lines with their rectangles, in the same order extract_text
reports the text (ADR 0008).
Sourcepub fn search(&self, needle: &str) -> Result<Vec<PageHits>>
pub fn search(&self, needle: &str) -> Result<Vec<PageHits>>
Every occurrence of needle in the document’s pages, case-insensitively.
The search runs over the spans, so a phrase is found inside a line and never across a line break: a reader searching “first line” wants the words, and a reader searching for text that only matches if two lines are glued together wants a different tool. An empty needle matches nothing rather than everything.
Sourcepub fn spans_from(
&self,
content: &[u8],
resources: Option<&Dictionary>,
) -> Result<Vec<TextSpan>>
pub fn spans_from( &self, content: &[u8], resources: Option<&Dictionary>, ) -> Result<Vec<TextSpan>>
Spans of a decoded content stream and its resource dictionary.
Source§impl Document
impl Document
Sourcepub fn open(data: impl Into<Vec<u8>>) -> Result<Self>
pub fn open(data: impl Into<Vec<u8>>) -> Result<Self>
Open a PDF from its bytes.
Accepts %PDF-1.x and %PDF-2.x files whose cross-reference is a classic
table, an
xref stream, or a hybrid of both, with any number of /Prev sections.
An encrypted file is opened with the empty password, which covers the
common “no password needed, but encrypted” case; use
Document::open_with_password otherwise.
Sourcepub fn open_with_password(
data: impl Into<Vec<u8>>,
password: &[u8],
) -> Result<Self>
pub fn open_with_password( data: impl Into<Vec<u8>>, password: &[u8], ) -> Result<Self>
Open a PDF with a password for its /V 5 (AES-256) security handler.
Sourcepub fn open_path_with_password(
path: impl AsRef<Path>,
password: &[u8],
) -> Result<Self>
pub fn open_path_with_password( path: impl AsRef<Path>, password: &[u8], ) -> Result<Self>
Open a PDF from a file path with a password.
Sourcepub fn is_encrypted(&self) -> bool
pub fn is_encrypted(&self) -> bool
Whether the document was opened with a security handler.
Sourcepub fn trailer(&self) -> &Dictionary
pub fn trailer(&self) -> &Dictionary
The newest trailer dictionary.
Sourcepub fn catalog(&self) -> Result<Object>
pub fn catalog(&self) -> Result<Object>
The document catalog, reached through the trailer’s /Root.
The catalogue is the entry point of the whole object graph (§7.7.2).
Sourcepub fn get_object(&self, id: ObjectId) -> Result<Object>
pub fn get_object(&self, id: ObjectId) -> Result<Object>
Fetch the object with this id, following the cross-reference.
Only the object number selects the entry, so a stale generation still
resolves — that is what real readers do and what incremental updates
rely on. Use Document::get_object_by_number to fetch with the
generation the cross-reference records.
Results are cached, so fetching the same object twice parses it once.
Sourcepub fn get_object_by_number(&self, number: u32) -> Result<Object>
pub fn get_object_by_number(&self, number: u32) -> Result<Object>
Fetch an object by number, using the generation the cross-reference has.
Sourcepub fn contains_object(&self, number: u32) -> bool
pub fn contains_object(&self, number: u32) -> bool
Whether the cross-reference knows number as a live object.
Sourcepub fn decoded_limit(&self) -> usize
pub fn decoded_limit(&self) -> usize
The decoded-bytes budget: the most any one stream may decode to.
Defaults to filter::DEFAULT_DECODE_LIMIT (256 MiB). A stream that
would decode past it fails with crate::Error::Limit instead of
allocating: without the cap a 1 MiB file can expand to a gigabyte, which
is a denial of service rather than a rendering failure.
Sourcepub fn set_decoded_limit(&self, bytes: usize)
pub fn set_decoded_limit(&self, bytes: usize)
Set the decoded-bytes budget, in bytes; usize::MAX lifts it.
Lower it when the caller knows its own ceiling (a browser tab, a server-side preview), or raise it for a document whose images legitimately decode to more than the default.
Sourcepub fn object_limit(&self) -> usize
pub fn object_limit(&self) -> usize
How many objects may be read before one is refused.
Defaults to DEFAULT_OBJECT_LIMIT. The count is of objects held
after being read, so a document that lists millions in its
cross-reference is still opened — the ceiling is on parsing them, which is
the part that costs memory.
Sourcepub fn set_object_limit(&self, objects: usize)
pub fn set_object_limit(&self, objects: usize)
Set the object ceiling; usize::MAX lifts it.
Sourcepub fn page_limit(&self) -> usize
pub fn page_limit(&self) -> usize
How many pages a page tree may list.
Defaults to DEFAULT_PAGE_LIMIT. Every pages
call walks the tree, so this bounds the work a crafted /Pages can ask
for.
Sourcepub fn set_page_limit(&self, pages: usize)
pub fn set_page_limit(&self, pages: usize)
Set the page ceiling; usize::MAX lifts it.
A document that legitimately has more pages than the default raises it, and a caller rendering one page at a time can lower it.
Sourcepub fn decode_stream(&self, stream: &Stream) -> Result<Vec<u8>>
pub fn decode_stream(&self, stream: &Stream) -> Result<Vec<u8>>
Decode a stream’s payload through its filter chain.
Indirect /Filter and /DecodeParms entries are resolved through the
object table first, which is what the plain Stream::decode cannot
do on its own.
Sourcepub fn stream_bytes(&self, id: ObjectId) -> Result<Vec<u8>>
pub fn stream_bytes(&self, id: ObjectId) -> Result<Vec<u8>>
Fetch a stream’s payload with decryption applied and its filters left alone.
A caller that brings its own decoder — the renderer’s /DCTDecode path —
needs the bytes the file holds, decrypted if the document is encrypted,
because Document::decode_stream would refuse an image filter.
Source§impl Document
impl Document
Sourcepub fn catalog_dict(&self) -> Result<Dictionary>
pub fn catalog_dict(&self) -> Result<Dictionary>
The catalog dictionary.
Sourcepub fn pages(&self) -> Result<Vec<Page>>
pub fn pages(&self) -> Result<Vec<Page>>
Walk the page tree and return every page in document order.
Sourcepub fn page_count(&self) -> Result<usize>
pub fn page_count(&self) -> Result<usize>
Number of pages, counted by walking the tree rather than trusting
/Count.
Sourcepub fn page_user_unit(&self, page: &Page) -> Result<f64>
pub fn page_user_unit(&self, page: &Page) -> Result<f64>
The page’s effective /UserUnit, defaulting to 1.0 (7.7.3.4).
The attribute is inheritable, so the page dictionary’s /Parent chain is
walked; a missing entry, a cycle (bounded at 32 levels) or a
non-positive value stops the walk at 1.0.
Sourcepub fn page_content(&self, page: &Page) -> Result<Vec<u8>>
pub fn page_content(&self, page: &Page) -> Result<Vec<u8>>
The decoded content of a page, with its streams joined by a newline.
This is the input the content-stream interpreter takes.
Sourcepub fn has_metadata(&self) -> Result<bool>
pub fn has_metadata(&self) -> Result<bool>
Whether the catalog carries an XMP /Metadata stream.
Source§impl Document
impl Document
Sourcepub fn new() -> DocumentBuilder
pub fn new() -> DocumentBuilder
Start a new, empty PDF 2.0 document.
Returns a DocumentBuilder rather than a reader: collect objects and
pages with it, then DocumentBuilder::save the file and
Document::open it back. The name follows the plan’s
Document::new() … save() shape; the builder is the only thing that
can be “new”, because a reader always comes from bytes.