JFLib

pdf/src/read/mod.rs#

Source: lib/pdf/src/read/mod.rs

pdf::read -- a PDF reader: the objects of a file, its pages, information, outlines, links and
other annotations, form fields, attachments, page labels, the text of each page with where
each glyph stands, and the pages drawn as pixels (render/).  PDF 1.0 to 2.0: classic cross-reference tables and cross-reference
streams, object streams, incremental updates, hybrid files; files whose table is missing or
wrong are rebuilt from their objects, as the common readers do.  Encrypted files with the
standard security handler open with their password, or without one when only the owner
password is set (RC4 40 to 128 bit, AES-128, AES-256).

  let doc = read::Document::open(bytes)?;            // or open_with(bytes, &Options { password, .. })
  for i in 0..doc.page_count() {
      let text = doc.text(i)?;                       // the page's text in reading order
      let size = doc.page(i)?.size();                // points, after /Rotate
  }
  let info = doc.info();                             // pdf::Info: title, author, dates, ...
  for o in doc.outlines() { .. }                     // bookmarks with their Action
  let px = doc.render(0, &RenderOptions { dpi: 150.0, ..Default::default() })?;  // gfx::Surface

  Document     open / open_with, version, is_encrypted / permissions / opened_as_owner,
               was_repaired, trailer / catalog, get(Ref) / resolve, decode(Stream) /
               decode_until_image, page_count / page / pages / contents, info / metadata /
               language, outlines, named_dest, annotations / links, fields, attachments,
               page_label, page_text (glyphs with positions) / text, page_pixels / render
               (RenderOptions: dpi, region, background, annotations, fonts: a FontSource,
               SystemFonts by default)
  Object       Null, Bool, Int, Real, String, Name, Array, Dict, Stream, Ref; text_string
  Ops          the operators of a content stream with their operands (content::Op)

Untrusted input: a damaged or hostile file gives an Error or less than it claims, never a
panic, a hang or memory without end.  Nesting, the objects of one parse, the depth of
references, page trees, outlines and form XObjects are bounded; every decoded stream is at
most Options::max_stream bytes.  Permissions are reported, not enforced: what a program
allows its user to do with the document is its own decision.

Composite fonts: every predefined CMap of ISO 32000-1 Table 118 (lib/cidmap: the code space,
code -> CID), and without a ToUnicode the text of Adobe's four CJK collections through their
CID -> Unicode tables (9.10.2); both unpacked once per document.

Not here: the image filters for decode_until_image (it stops before DCT, JPX, JBIG2 and
CCITTFax; drawing decodes DCT, CCITTFax, JPX and JBIG2), verifying signatures.  pdf::edit
changes a document and saves it as an incremental update (forms filled in and flattened).
  • struct Options how a document is opened

    • password: String -- the user or the owner password ("" for files without one)
    • max_stream: usize -- the largest decoded stream, in bytes (a content stream, an image, an attachment)
  • enum Error why a document or a part of it cannot be read

    • NotPdf -- not a PDF file: no header and no objects
    • Damaged(&'static str) -- the file is damaged where it was needed
    • Password -- encrypted, and the password is missing or wrong
    • Unsupported(&'static str) -- something this reader does not do (a security handler other than the standard one, an image filter)
    • Invalid(&'static str) -- a change pdf::edit cannot make: a field that is not there, a value it does not take
    • Limit(&'static str) -- more than the Options allow
    • NoPage -- a page number past the last page
  • struct Page a page: its boxes (points, PDF page space with y up), rotation and dictionaries

    • obj: Option<Ref> -- the page object
    • media_box: [f64; 4] -- x0, y0, x1, y1
    • crop_box: [f64; 4] -- the visible part (the media box when the page sets none)
    • rotate: u16 -- clockwise when shown: 0, 90, 180 or 270
    • user_unit: f64 -- the size of a unit in points (PDF 1.6; usually 1)
    • dict: Dict -- the page dictionary, and its resources (inherited ones included)
    • resources: Dict

impl Page

  • fn size(&self) -> (f64, f64) width and height of the crop box in points as the page is shown (after /Rotate)

  • struct Document a PDF document opened for reading

impl Document

  • fn open(data: Vec<u8>) -> Result<Document, Error> open a document without a password

  • fn open_with(data: Vec<u8>, opt: &Options) -> Result<Document, Error>

  • fn version(&self) -> (u8, u8) the version of the header, or of the catalog's /Version when that is newer

  • fn is_encrypted(&self) -> bool

  • fn permissions(&self) -> Option<u32> the permission bits /P of an encrypted file (ISO 32000-1, Table 22: bit 3 print, 4 modify, 5 copy text and graphics, 6 annotate, 9 fill in forms, 10 extract for accessibility, 11 assemble, 12 print in high quality; counted from 1)

  • fn encryption_revision(&self) -> Option<u8> the revision of the standard security handler (2 to 6) of an encrypted file

  • fn opened_as_owner(&self) -> bool the password given was the owner password

  • fn was_repaired(&self) -> bool the cross-reference was rebuilt from the objects of the file

  • fn trailer(&self) -> &Dict

  • fn catalog(&self) -> Dict the document catalog (empty when there is none)

  • fn data(&self) -> &[u8] the raw bytes of the file

  • fn get(&self, r: Ref) -> Object an indirect object (Null when it is missing, as ISO 32000 has it)

  • fn resolve(&self, o: &Object) -> Object follow references until an object that is none (Null after too many)

  • fn decode(&self, s: &Stream) -> Result<Vec<u8>, Error>

  • fn decode_until_image(&self, s: &Stream) -> Result<(Vec<u8>, Option<(Name, Dict)>), Error> a stream's data with its filters applied up to an image filter: the data, and the image filter with its parameters (DCTDecode is a JPEG file then, JPXDecode a JPEG 2000 one)

  • fn page_count(&self) -> usize

  • fn pages(&self) -> &[Page]

  • fn page(&self, i: usize) -> Result<&Page, Error>

  • fn page_of(&self, r: Ref) -> Option<usize> the page a page object is (for destinations)

  • fn contents(&self, i: usize) -> Result<Vec<u8>, Error> the content of a page: its content streams decoded and joined (a stream that cannot be decoded adds nothing)

  • fn info(&self) -> Info the document information (the Info dictionary; dates as the file writes them)

  • fn metadata(&self) -> Option<Vec<u8>> the XMP metadata of the document (the catalog's /Metadata stream)

  • fn language(&self) -> Option<String> the natural language of the document (/Lang: "de-DE")

  • fn parse_date(s: &str) -> Option<Date> a date of a PDF file (7.9.4: "D:YYYYMMDDHHmmSSOHH'mm'"; every part after the year may be missing, as may "D:")

Source: docs/API.md in the JFLib repository.