Skip to main content

Crate rusty_pdf_core

Crate rusty_pdf_core 

Source
Expand description

rusty_pdf_core reads, edits and saves PDF files: the object layer that a renderer and a writer are built on.

use rusty_pdf_core::{dictionary, Document, Object, SaveMode, Stream};

// Build a one-page document, save it, and read it back.
let mut doc = Document::new();
let content = doc.add_object(Stream::new(dictionary! {}, b"0 0 100 100 re f".to_vec()));
let pages = doc.catalog()?.get(b"Pages").unwrap().as_reference()?;
let page = doc.add_object(dictionary! {
    "Type" => "Page",
    "Parent" => pages,
    "MediaBox" => vec![0.into(), 0.into(), 612.into(), 792.into()],
    "Contents" => content,
});
let tree = doc.get_object_mut(pages)?.as_dict_mut()?;
tree.set("Kids", vec![Object::Reference(page)]);
tree.set("Count", 1);
let bytes = doc.save(SaveMode::Rewrite)?;

let doc = Document::load(&bytes)?;
assert_eq!(doc.page_count(), 1);
let page = doc.page(0)?;
assert_eq!(page.media_box().width(), 612.0);
assert_eq!(page.operations()?.last().unwrap().operator, "f");

§What it reads

Classic cross-reference tables, cross-reference streams, hybrid files, object streams and incremental updates. When the cross-reference data is missing or wrong, the file is scanned and the table rebuilt (Document::was_repaired). Encrypted files open with the standard security handler: RC4 at 40 to 128 bits, AES-128 and AES-256.

§Hostile input

A PDF is attacker-controlled data. Malformed input returns an Error or a bounded result; it does not panic, loop without progress, or allocate past Limits. Every stream decode takes a byte cap, and no allocation is sized from a number in the file without a check first.

Modules§

text
PDF text strings: PDFDocEncoding and UTF-16BE (7.9.2.2).

Macros§

dictionary
Build a Dictionary from "Key" => value pairs. A &str value becomes a name.

Structs§

Content
A parsed content stream.
Dictionary
A dictionary: names to objects.
Document
A PDF document.
Image
A decoded image.
ImageOptions
Bounds and policy for decoding one image.
LenientImage
An image from Document::decode_image_lenient, with what was decoded around to produce it.
Limits
Bounds on what a file may ask of the reader.
LoadOptions
Options for Document::load_with.
Operation
One operator with its operands, as written: 10 20 m is operator m with operands [10, 20].
Operator
A content-stream operator: one of the standard operators (no allocation) or, for anything else a file contains, its own text.
Page
One page of a Document.
Rect
A rectangle in default user space, normalised so that x0 <= x1 and y0 <= y1.
Stream
A stream: a dictionary and the bytes it describes.
ToUnicode
A parsed ToUnicode map.

Enums§

ColorSpace
What an image’s samples mean.
Error
Why a PDF could not be read, decoded or written.
Object
A PDF object.
SaveMode
How Document::save writes the file.
StringFormat
How a string was (or should be) written. It does not change the bytes.

Functions§

flate_encode
Compress plain as a zlib stream.

Type Aliases§

ObjectId
An indirect object’s identity: object number and generation.
Result
Result with this crate’s Error.