Skip to main content

PdfBackend

Trait PdfBackend 

Source
pub trait PdfBackend {
    type Document;
    type Page;
    type Error: Error + Into<PdfError>;

    // Required methods
    fn open(bytes: &[u8]) -> Result<Self::Document, Self::Error>;
    fn page_count(doc: &Self::Document) -> usize;
    fn get_page(
        doc: &Self::Document,
        index: usize,
    ) -> Result<Self::Page, Self::Error>;
    fn page_media_box(
        doc: &Self::Document,
        page: &Self::Page,
    ) -> Result<BBox, Self::Error>;
    fn page_crop_box(
        doc: &Self::Document,
        page: &Self::Page,
    ) -> Result<Option<BBox>, Self::Error>;
    fn page_rotate(
        doc: &Self::Document,
        page: &Self::Page,
    ) -> Result<i32, Self::Error>;
    fn interpret_page(
        doc: &Self::Document,
        page: &Self::Page,
        handler: &mut dyn ContentHandler,
        options: &ExtractOptions,
    ) -> Result<(), Self::Error>;
}
Expand description

Trait abstracting PDF parsing operations.

A backend provides methods to open PDF documents, access pages, extract page properties (MediaBox, CropBox, Rotate), and interpret page content streams via a ContentHandler callback.

§Associated Types

  • Document: The parsed PDF document representation.
  • Page: A reference to a single page within a document.
  • Error: Backend-specific error type, convertible to PdfError.

§Usage

let doc = MyBackend::open(pdf_bytes)?;
let page_count = MyBackend::page_count(&doc);
let page = MyBackend::get_page(&doc, 0)?;
let media_box = MyBackend::page_media_box(&doc, &page)?;
MyBackend::interpret_page(&doc, &page, &mut handler, &options)?;

Required Associated Types§

Source

type Document

The parsed PDF document type.

Source

type Page

A reference to a single page within a document.

Source

type Error: Error + Into<PdfError>

Backend-specific error type, convertible to PdfError.

Required Methods§

Source

fn open(bytes: &[u8]) -> Result<Self::Document, Self::Error>

Parse PDF bytes into a document.

§Errors

Returns an error if the bytes do not represent a valid PDF document.

Source

fn page_count(doc: &Self::Document) -> usize

Return the number of pages in the document.

Source

fn get_page( doc: &Self::Document, index: usize, ) -> Result<Self::Page, Self::Error>

Access a page by 0-based index.

§Errors

Returns an error if the index is out of range or the page cannot be loaded.

Source

fn page_media_box( doc: &Self::Document, page: &Self::Page, ) -> Result<BBox, Self::Error>

Get the MediaBox for a page.

MediaBox is required by the PDF specification and defines the boundaries of the physical page. The returned BBox uses the library’s top-left origin coordinate system.

§Errors

Returns an error if the MediaBox cannot be resolved (e.g., missing from both the page and its parent page tree).

Source

fn page_crop_box( doc: &Self::Document, page: &Self::Page, ) -> Result<Option<BBox>, Self::Error>

Get the CropBox for a page, if explicitly set.

CropBox defines the visible region of the page. Returns None if not explicitly set (in which case MediaBox serves as the CropBox).

§Errors

Returns an error if the CropBox entry exists but is malformed.

Source

fn page_rotate( doc: &Self::Document, page: &Self::Page, ) -> Result<i32, Self::Error>

Get the page rotation angle in degrees.

Returns one of: 0, 90, 180, or 270. Defaults to 0 if not specified.

§Errors

Returns an error if the Rotate entry exists but is malformed.

Source

fn interpret_page( doc: &Self::Document, page: &Self::Page, handler: &mut dyn ContentHandler, options: &ExtractOptions, ) -> Result<(), Self::Error>

Interpret the page’s content stream, calling back into the handler.

The interpreter processes PDF content stream operators (text, path, image) and notifies the handler of extracted content via ContentHandler callbacks. Resource limits from options are enforced during interpretation.

§Errors

Returns an error if content stream parsing fails or a resource limit is exceeded.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§