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.
- Load
Options - How to open a document.
- ObjStm
- A decoded object stream and its member table.
- ObjStm
Entry - One member of an object stream.
- Object
Store - Everything needed to turn a reference into an object.
- Page
Dict - One page’s dictionary, with the attributes it inherits already resolved.
- Section
- One cross-reference section of the file, as the
/Prevchain found it. - Trailer
- The trailer dictionary plus which object it came out of.
- Xref
- Where every object in a document lives.
Enums§
- Char
Class - 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.
- Load
Error - Why a document could not be opened.
- Strictness
- How much malformed syntax an object parse tolerates.
- Token
- One syntactic token, borrowing from the file.
- Word
Boundary - 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/Contentselement — read throughr. - decoded_
stream - A stream’s data with its filters applied.
- find_
word - Find
wordat or afterfrom, as a whole word underrule(seeis_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
streamdata and%%EOFscanning. - is_
numeric - Whether a byte may appear in a number:
0–9,+,-,.. - is_
whitespace - Whether a byte separates tokens.
- is_
whole_ word - Whether the
lenbytes atposstand alone as a word underrule. - load
- Open a document.
- parse_
indirect_ object - Parse
N G obj … endobjat 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
%%EOFthat closes the revision whosestartxrefnamesoffset.