Skip to main content

Crate pdfrum_parser

Crate pdfrum_parser 

Source
Expand description

§pdfrum-parser

The file layer (ISO 32000-1 §7.5). A PDF is %PDF-m.n, a body of objects, a cross-reference table or stream at the end, and a trailer pointing at both; an incremental update appends another body, xref and trailer in front of the last. load finds that chain, or gives up on it and rebuilds the table by scanning the whole file for N G obj.

use std::sync::Arc;
use pdfrum_parser::{LoadOptions, load};

let bytes = std::fs::read(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/files/minimal.pdf"))?;
let doc = load(Arc::from(bytes), &LoadOptions::default())?;
assert_eq!(doc.page_count(), 1);

A malformed file PDFium would open is not an error here. That is the whole design constraint: real PDFs have wrong /Length values, xref offsets off by a few bytes, tables that point at nothing, and generations that do not match. Every one of those is repaired and recorded in the document’s diagnostics, and Err is reserved for a file that cannot be opened at all. A caller that wants strictness reads the diagnostics; a caller that wants the document gets the document.

Loading is lazy. load reads the header, the xref chain and the trailer, and leaves object bodies as byte offsets until something asks for them — so opening a thousand-page file to read its page count does not parse a thousand pages, and the object store is Arc-backed shared bytes rather than a parsed tree in memory.

The stages are separable on purpose and each is testable on values alone: the lexer turns bytes into tokens and owns the byte classifier everything else asks questions of, syntax turns tokens into Objects and is where stream /Length repair lives, and xref finds cross-reference information by every route a file can offer before rebuilding from scratch.

Part of pdfrum. #![forbid(unsafe_code)].

MIT OR Apache-2.0

Structs§

Document
An opened document.
Indirect
One indirect object: its number, generation, and body.
Lexer
A cursor over the bytes of a PDF file.
LoadOptions
How to open a document.
ObjStm
A decoded object stream and its member table.
ObjStmEntry
One member of an object stream.
ObjectStore
Everything needed to turn a reference into an object.
PageDict
One page’s dictionary, with the attributes it inherits already resolved.
Section
One cross-reference section of the file, as the /Prev chain found it.
Trailer
The trailer dictionary plus which object it came out of.
Xref
Where every object in a document lives.

Enums§

CharClass
What a byte means to the tokenizer.
Delim
Punctuation the tokenizer recognizes.
Entry
Where one object lives.
Error
A failure below the document level: a token that is not an object, a cross-reference section that cannot be read, a fetch that finds nothing.
LoadError
Why a document could not be opened.
Strictness
How much malformed syntax an object parse tolerates.
Token
One syntactic token, borrowing from the file.
WordBoundary
Which bytes may neighbour a match for it to stand alone as a word.

Functions§

atoi64
Parse a signed decimal into an i64, saturating rather than wrapping.
atoui
Parse an unsigned decimal the way the C library’s conversion does, which is what every count and offset in a cross-reference table goes through.
class_of
The class of one byte.
content_segments
The content of dict’s page, decoded and joined, with the end offset of each /Contents element — read through r.
decoded_stream
A stream’s data with its filters applied.
find_word
Find word at or after from, as a whole word under rule (see is_whole_word). Returns the offset of its first byte.
is_delimiter
Whether a byte ends a token and starts syntax of its own.
is_line_ending
Whether a byte ends a line, for the purposes of stream data and %%EOF scanning.
is_numeric
Whether a byte may appear in a number: 09, +, -, ..
is_whitespace
Whether a byte separates tokens.
is_whole_word
Whether the len bytes at pos stand alone as a word under rule.
load
Open a document.
parse_indirect_object
Parse N G obj … endobj at the lexer’s position.
parse_object
Parse one object body at the lexer’s position.
read_xref
Read a document’s cross-reference information, rebuilding it if needed.
revision_end
The byte after the %%EOF that closes the revision whose startxref names offset.