Rust API Every item on the facade, with its signature, parameters and errors. This surface was designed first; the JavaScript one wraps it.
The API is synchronous. Nothing in the library does IO, so there is nothing to await. The asynchrony on the JavaScript side is the Worker boundary, not the work.
Crate layering Each crate is publishable and usable alone. Dependencies only go upward: nothing in rasura-cos knows what a paragraph is, and nothing in rasura-layout knows how to write a file.
rasura-cos objects, xref, filters, decryption, the writer
rasura-content content streams, graphics and text state, layers
rasura-font parsing, shaping, subsetting, injection, embedding
rasura-layout glyph runs to lines to blocks to a document model
rasura-edit edit operations, reflow, stream patching, sessions
rasura-flow the flow model, the layout engine, composition
rasura the facade documented on this page
rasura-wasm the wasm-bindgen surface
[dependencies]
rasura = "0.1"
# Or one layer, if that is all you need:
rasura-cos = "0.1"
Opening and creating On Document.
pub fn open(bytes: Vec<u8>) -> Result<Document>Parse a PDF with default options. pub fn open_with(bytes: Vec<u8>, opts: &OpenOptions) -> Result<Document>Parse a PDF with a password, or with recovery disabled. pub fn create(content: &[Content], opts: &create::Options) -> Result<(Document, Composition)>Compose a document that did not exist. Reading On Document.
pub fn page_count(&self) -> usizeHow many pages the document has.
pub fn page(&self, index: usize) -> Result<Page>Reconstruct one page into the document model. pub fn kind(&self) -> DocumentKindBornDigital, Scanned or Mixed. pub fn tagged_status(&self) -> TaggedStatusWhether a structure tree is present, absent, or degraded by editing.
pub fn leniencies(&self) -> Vec<Leniency>Every specification deviation tolerated to open this file. pub fn permissions(&self) -> PermissionsWhat the /P bits say a reader should allow. pub fn has_xfa(&self) -> boolWhether the AcroForm carries an XFA packet. Form edits are refused if so.
pub fn is_encrypted(&self) -> boolWhether the file was encrypted when it was opened.
pub fn revision_count(&self) -> usizeHow many incremental revisions the file contains.
pub fn fonts(&self) -> Vec<FontInfo>What each font can and cannot draw. pub fn metadata(&self) -> MetadataThe /Info dictionary and the XMP packet, separately. pub fn form_fields(&self) -> Vec<Field>Every AcroForm field, by fully qualified name.
pub fn memory_usage(&self) -> usizeBytes held: the source buffer, the object cache and decoded streams.
Whole-document operations On Document.
pub fn edit(&mut self) -> Session<'_>Begin an edit session. Everything staged in it shares one undo stack. pub fn redact(&mut self, text: &str) -> Result<Vec<String>>Remove every occurrence of a string from the document. pub fn verify_redaction(bytes: &[u8], strings: &[String]) -> ReportSearch saved bytes for strings that should be gone. pub fn protect(&mut self, policy: &Policy, entropy: &[u8; 32]) -> Result<Vec<Weakness>>Encrypt the document, or change its passwords. pub fn unprotect(&mut self) -> Result<()>Remove encryption. Forces a full rewrite.
pub fn register_font(&mut self, bytes: Vec<u8>, opts: &RegisterOptions) -> Result<FontHandle>Supply a typeface for glyphs the document cannot draw. pub fn compact_fonts(&mut self) -> Result<usize>Drop glyphs no page draws from every embedded font. pub fn save(&self, opts: &SaveOptions) -> Result<Saved>Write the file. pub fn raw(&self) -> &rasura_cos::DocumentThe object layer underneath, for anything this surface does not cover. Session On Session<'a>.
pub fn require(&mut self, floor: Fidelity) -> &mut SelfRefuse any operation that cannot reach this rung. pub fn reflow(&mut self, breaking: Breaking, overflow: Overflow) -> &mut SelfHow lines are re-broken, and what happens when text no longer fits. pub fn replace_text(&mut self, page: &Page, id: ParagraphId, range: Range<usize>, text: &str) -> Result<Outcome>Replace a character range within one paragraph. pub fn insert_text(&mut self, page: &Page, id: ParagraphId, at: usize, text: &str) -> Result<Outcome>Insert at a character offset.
pub fn delete_range(&mut self, page: &Page, id: ParagraphId, range: Range<usize>) -> Result<Outcome>Delete a character range.
pub fn move_image(&mut self, page: &Page, id: ImageId, dx: f64, dy: f64) -> Result<Outcome>Translate an image by a delta in points. pub fn scale_image(&mut self, page: &Page, id: ImageId, sx: f64, sy: f64) -> Result<Outcome>Scale an image about its own origin.
pub fn delete_image(&mut self, page: &Page, id: ImageId) -> Result<Outcome>Remove the drawing operator, leaving the object for anything else that draws it.
pub fn set_cell(&mut self, page: &Page, table: usize, row: usize, col: usize, text: &str) -> Result<Outcome>Replace the text of one detected table cell.
pub fn delete_page(&mut self, index: usize) -> Result<Outcome>Remove a page and retarget everything pointing at it. pub fn move_page(&mut self, from: usize, to: usize) -> Result<Outcome>Reorder pages, fixing navigation.
pub fn add_annotation(&mut self, page: &Page, new: NewAnnotation) -> Result<Outcome>Create an annotation with a generated appearance stream. pub fn delete_annotation(&mut self, page: &Page, id: ObjId) -> Result<Outcome>Remove an annotation and its entry in /Annots.
pub fn set_field_value(&mut self, name: &str, value: &str) -> Result<Outcome>Fill a form field by fully qualified name and regenerate its appearance. pub fn flatten_forms(&mut self, page: &Page) -> Result<Outcome>Turn field values into page content. pub fn undo(&mut self) -> Result<bool>Reverse the last staged operation exactly. pub fn redo(&mut self) -> Result<bool>Reapply the last undone operation.
pub fn rollback(&mut self) -> Result<()>Discard everything staged and close the session.
pub fn fidelity(&self) -> FidelityThe lowest rung any staged operation reached.
pub fn commit(&mut self, opts: &SaveOptions) -> Result<Saved>Apply everything staged and write the file.
pub fn suspend(self) -> SessionStateDetach a session so the document can be borrowed elsewhere, then continue it. Types pub enum Fidelity { Exact , Reembedded , Substituted , Overlaid }
pub struct Outcome {
pub fidelity : Fidelity ,
pub compromises : Vec < Compromise > ,
pub retargeted : usize ,
}
pub struct Saved {
pub bytes : Vec < u8 > ,
pub mode : SaveMode ,
pub bytes_appended : usize ,
pub warnings : Vec < Warning > ,
}
pub struct OpenOptions { pub password : String , pub recovery : Recovery }
pub enum Recovery { Auto , Never }
pub struct SaveOptions {
pub mode : Option < SaveMode > ,
pub accept_signature_destruction : bool ,
}
pub enum SaveMode { Incremental , FullRewrite }
pub enum DocumentKind { BornDigital , Scanned , Mixed }
pub enum TaggedStatus { Tagged , Untagged , Degraded }
Composition has its own small set:
pub enum Content {
Heading { level : u8 , text : String } ,
Paragraph { text : String } ,
List { items : Vec < String > } ,
}
pub struct PageGeometry {
pub size : ( f64 , f64 ) ,
pub margins : ( f64 , f64 , f64 , f64 ) ,
pub columns : usize ,
pub gutter : f64 ,
}
pub struct Composition {
pub pages : usize ,
pub lines : usize ,
pub approximated : usize ,
pub missing : Vec < char > ,
pub base_font : String ,
pub composite : bool ,
pub stem_v_estimated : bool ,
}
Errors One error type, always coded. The layers below have their own richer enums; those are kept in Error::detail rather than discarded, because throwing the cause away to keep the surface clean makes debugging somebody else’s PDF impossible.
pub struct Error { }
impl Error {
pub fn code ( & self ) -> Code ;
pub fn message ( & self ) -> & str ;
pub fn detail ( & self ) -> & str ;
}
pub enum Code {
Malformed ,
EncryptedPasswordRequired ,
EncryptedUnsupported ,
ScannedNoText ,
XfaUnsupported ,
Type3GlyphMissing ,
FontUnavailable ,
Overflow ,
StaleSession ,
FidelityBelowRequired ,
SignatureWouldBeDestroyed ,
UnsupportedFilter ,
InvalidArgument ,
Internal ,
}
Matching on codes
Code is Copy and PartialEq, so a match on e.code() is the intended way to branch. The string forms used by the TypeScript surface come from Code::as_str and are written out rather than derived, because a rename that silently changed one would break every caller.