PdfElixide.Document (pdf_elixide v0.4.0)

Copy Markdown View Source

Read-only representation of a PDF document.

Summary

Functions

Authenticates against the document's encryption with the given password.

Same as authenticate/2 but raises on error.

Returns whether the PDF document is encrypted.

Extracts the text content of the page at the given zero-based index.

Extracts the text content of the page at the given zero-based index, raising an error if it fails.

Opens a PDF document from the given binary data.

Opens a PDF document from the given binary data, raising an error if it fails.

Returns whether the PDF document is a Tagged PDF with a structure tree.

Opens a PDF document from the specified file path.

Opens a PDF document from the specified file path, raising an error if it fails.

Returns the number of pages in the given PDF document.

Returns the number of pages in the given PDF document, raising an error if it fails.

Returns the file path from which the document was loaded, or nil if it was loaded from binary data.

Returns the PDF specification version of the given document as a {major, minor} tuple.

Types

open_opts()

@type open_opts() :: [{:password, String.t()}]

Options accepted by open/2, open!/2, from_binary/2, and from_binary!/2.

  • :password — password used to authenticate against an encrypted PDF. When the password is wrong, the call returns {:error, "Authentication failed: wrong password"} (or raises, for the bang variants). When omitted or nil, no authentication attempt is made beyond pdf_oxide's built-in empty-password try.

t()

@type t() :: %PdfElixide.Document{
  ref: reference(),
  source_path: Path.t() | nil,
  version: {non_neg_integer(), non_neg_integer()}
}

Functions

authenticate(document, password)

@spec authenticate(t(), binary()) :: {:ok, boolean()} | {:error, term()}

Authenticates against the document's encryption with the given password.

Returns {:ok, true} if authentication succeeded (or the PDF is not encrypted), {:ok, false} if the password was wrong, or {:error, reason} on a PDF/crypto error.

authenticate!(doc, password)

@spec authenticate!(t(), binary()) :: boolean()

Same as authenticate/2 but raises on error.

Still returns false (does not raise) for a wrong password.

encrypted?(document)

@spec encrypted?(t()) :: boolean()

Returns whether the PDF document is encrypted.

extract_text(document, page_index)

@spec extract_text(t(), non_neg_integer()) :: {:ok, binary()} | {:error, term()}

Extracts the text content of the page at the given zero-based index.

extract_text!(doc, page_index)

@spec extract_text!(t(), non_neg_integer()) :: binary()

Extracts the text content of the page at the given zero-based index, raising an error if it fails.

from_binary(bytes, opts \\ [])

@spec from_binary(binary(), open_opts()) :: {:ok, t()} | {:error, term()}

Opens a PDF document from the given binary data.

from_binary!(bytes, opts \\ [])

@spec from_binary!(binary(), open_opts()) :: t()

Opens a PDF document from the given binary data, raising an error if it fails.

has_structure_tree?(document)

@spec has_structure_tree?(t()) :: boolean()

Returns whether the PDF document is a Tagged PDF with a structure tree.

open(path, opts \\ [])

@spec open(Path.t(), open_opts()) :: {:ok, t()} | {:error, term()}

Opens a PDF document from the specified file path.

open!(path, opts \\ [])

@spec open!(Path.t(), open_opts()) :: t()

Opens a PDF document from the specified file path, raising an error if it fails.

page_count(document)

@spec page_count(t()) :: {:ok, non_neg_integer()} | {:error, term()}

Returns the number of pages in the given PDF document.

page_count!(doc)

@spec page_count!(t()) :: non_neg_integer()

Returns the number of pages in the given PDF document, raising an error if it fails.

source_path(document)

@spec source_path(t()) :: Path.t() | nil

Returns the file path from which the document was loaded, or nil if it was loaded from binary data.

version(document)

@spec version(t()) :: {non_neg_integer(), non_neg_integer()}

Returns the PDF specification version of the given document as a {major, minor} tuple.