PdfElixide.Document (pdf_elixide v0.7.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.

Extracts the characters of the whole document.

Extracts the characters of the page at the given zero-based index.

Extracts the characters of the whole document, raising an error if it fails.

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

Returns whether the PDF document is encrypted.

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.

Extracts the raster images of the whole document.

Extracts the raster images of the page at the given zero-based index.

Extracts the raster images of the whole document, raising an error if it fails.

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

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 a lazy handle for the page at the given zero-based index.

Same as page/2 but raises 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 a lazy handle for every page in the document.

Extracts the vector paths of the whole document.

Extracts the vector paths of the page at the given zero-based index.

Extracts the vector paths of the whole document, raising an error if it fails.

Extracts the vector paths of the page at the given zero-based index, 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.

Extracts the spans of the whole document.

Extracts the spans of the page at the given zero-based index.

Extracts the spans of the whole document, raising an error if it fails.

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

Detects the tables of the whole document.

Detects the tables of the page at the given zero-based index.

Detects the tables of the whole document, raising an error if it fails.

Detects the tables of the page at the given zero-based index, raising an error if it fails.

Extracts the text content of the whole document.

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

Extracts the text content of the whole document, raising an error if it fails.

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

Extracts the text lines of the whole document.

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

Extracts the text lines of the whole document, raising an error if it fails.

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

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

Extracts the words of the whole document.

Extracts the words of the page at the given zero-based index.

Extracts the words of the whole document, raising an error if it fails.

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

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, %PdfElixide.Error{reason: :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: PdfElixide.Document.Path.t() | nil,
  version: {non_neg_integer(), non_neg_integer()}
}

Functions

authenticate(document, password)

@spec authenticate(t(), binary()) :: {:ok, boolean()} | {:error, PdfElixide.Error.t()}

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.

chars(document)

@spec chars(t()) ::
  {:ok, [PdfElixide.Document.Char.t()]} | {:error, PdfElixide.Error.t()}

Extracts the characters of the whole document.

Returns every page's characters concatenated into a single flat list, in page order. Each character carries its bounding box, font metadata, and typographic placement as a PdfElixide.Document.Char struct.

chars(document, page_index)

@spec chars(t(), non_neg_integer()) ::
  {:ok, [PdfElixide.Document.Char.t()]} | {:error, PdfElixide.Error.t()}

Extracts the characters of the page at the given zero-based index.

Each character carries its bounding box, font metadata, and typographic placement as a PdfElixide.Document.Char struct.

chars!(doc)

@spec chars!(t()) :: [PdfElixide.Document.Char.t()]

Extracts the characters of the whole document, raising an error if it fails.

chars!(doc, page_index)

@spec chars!(t(), non_neg_integer()) :: [PdfElixide.Document.Char.t()]

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

encrypted?(document)

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

Returns whether the PDF document is encrypted.

from_binary(bytes, opts \\ [])

@spec from_binary(binary(), open_opts()) ::
  {:ok, t()} | {:error, PdfElixide.Error.t()}

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.

images(document)

@spec images(t()) ::
  {:ok, [PdfElixide.Document.Image.t()]} | {:error, PdfElixide.Error.t()}

Extracts the raster images of the whole document.

Returns every page's images concatenated into a single flat list, in page order, as PdfElixide.Document.Image structs. Pixel data is normalized to PNG bytes.

images(document, page_index)

@spec images(t(), non_neg_integer()) ::
  {:ok, [PdfElixide.Document.Image.t()]} | {:error, PdfElixide.Error.t()}

Extracts the raster images of the page at the given zero-based index.

Returns {:ok, []} when the page has no images. Each image — a photo, logo, or scanned picture — is carried as a PdfElixide.Document.Image struct with its pixel data normalized to PNG bytes.

images!(doc)

@spec images!(t()) :: [PdfElixide.Document.Image.t()]

Extracts the raster images of the whole document, raising an error if it fails.

images!(doc, page_index)

@spec images!(t(), non_neg_integer()) :: [PdfElixide.Document.Image.t()]

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

open(path, opts \\ [])

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

Opens a PDF document from the specified file path.

open!(path, opts \\ [])

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

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

page(doc, index)

@spec page(t(), non_neg_integer()) ::
  {:ok, PdfElixide.Document.Page.t()} | {:error, PdfElixide.Error.t()}

Returns a lazy handle for the page at the given zero-based index.

page!(doc, index)

Same as page/2 but raises an error if it fails.

page_count(document)

@spec page_count(t()) :: {:ok, non_neg_integer()} | {:error, PdfElixide.Error.t()}

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.

pages(doc)

@spec pages(t()) :: [PdfElixide.Document.Page.t()]

Returns a lazy handle for every page in the document.

paths(document)

@spec paths(t()) ::
  {:ok, [PdfElixide.Document.Path.t()]} | {:error, PdfElixide.Error.t()}

Extracts the vector paths of the whole document.

Returns every page's paths concatenated into a single flat list, in page order, as PdfElixide.Document.Path structs.

paths(document, page_index)

@spec paths(t(), non_neg_integer()) ::
  {:ok, [PdfElixide.Document.Path.t()]} | {:error, PdfElixide.Error.t()}

Extracts the vector paths of the page at the given zero-based index.

Returns {:ok, []} when the page has no vector graphics. Each path — a line, curve, rectangle, or filled shape — is carried as a PdfElixide.Document.Path struct.

paths!(doc)

@spec paths!(t()) :: [PdfElixide.Document.Path.t()]

Extracts the vector paths of the whole document, raising an error if it fails.

paths!(doc, page_index)

@spec paths!(t(), non_neg_integer()) :: [PdfElixide.Document.Path.t()]

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

source_path(document)

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

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

spans(document)

@spec spans(t()) ::
  {:ok, [PdfElixide.Document.Span.t()]} | {:error, PdfElixide.Error.t()}

Extracts the spans of the whole document.

Returns every page's spans concatenated into a single flat list, in page order. Each span is a run of text sharing one text state, carried as a PdfElixide.Document.Span struct.

spans(document, page_index)

@spec spans(t(), non_neg_integer()) ::
  {:ok, [PdfElixide.Document.Span.t()]} | {:error, PdfElixide.Error.t()}

Extracts the spans of the page at the given zero-based index.

Each span is a run of text sharing one text state, carried as a PdfElixide.Document.Span struct.

spans!(doc)

@spec spans!(t()) :: [PdfElixide.Document.Span.t()]

Extracts the spans of the whole document, raising an error if it fails.

spans!(doc, page_index)

@spec spans!(t(), non_neg_integer()) :: [PdfElixide.Document.Span.t()]

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

tables(document)

@spec tables(t()) ::
  {:ok, [PdfElixide.Document.Table.t()]} | {:error, PdfElixide.Error.t()}

Detects the tables of the whole document.

Returns every page's tables concatenated into a single flat list, in page order, as PdfElixide.Document.Table structs.

Detection is heuristic — see PdfElixide.Document.Table for the :real_grid? flag and how to filter out likely false positives.

tables(document, page_index)

@spec tables(t(), non_neg_integer()) ::
  {:ok, [PdfElixide.Document.Table.t()]} | {:error, PdfElixide.Error.t()}

Detects the tables of the page at the given zero-based index.

Returns {:ok, []} when the page has no detectable table. Detection is heuristic — see PdfElixide.Document.Table for the :real_grid? flag and how to filter out likely false positives.

tables!(doc)

@spec tables!(t()) :: [PdfElixide.Document.Table.t()]

Detects the tables of the whole document, raising an error if it fails.

tables!(doc, page_index)

@spec tables!(t(), non_neg_integer()) :: [PdfElixide.Document.Table.t()]

Detects the tables of the page at the given zero-based index, raising an error if it fails.

text(document)

@spec text(t()) :: {:ok, binary()} | {:error, PdfElixide.Error.t()}

Extracts the text content of the whole document.

Returns every page's text concatenated in order, separated by a form-feed (\f) page separator.

text(document, page_index)

@spec text(t(), non_neg_integer()) :: {:ok, binary()} | {:error, PdfElixide.Error.t()}

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

text!(doc)

@spec text!(t()) :: binary()

Extracts the text content of the whole document, raising an error if it fails.

text!(doc, page_index)

@spec 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.

text_lines(document)

@spec text_lines(t()) ::
  {:ok, [PdfElixide.Document.TextLine.t()]} | {:error, PdfElixide.Error.t()}

Extracts the text lines of the whole document.

Returns every page's lines concatenated into a single flat list, in page order. Each line carries its bounding box and constituent words as a PdfElixide.Document.TextLine struct.

text_lines(document, page_index)

@spec text_lines(t(), non_neg_integer()) ::
  {:ok, [PdfElixide.Document.TextLine.t()]} | {:error, PdfElixide.Error.t()}

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

Each line carries its bounding box and constituent words as a PdfElixide.Document.TextLine struct.

text_lines!(doc)

@spec text_lines!(t()) :: [PdfElixide.Document.TextLine.t()]

Extracts the text lines of the whole document, raising an error if it fails.

text_lines!(doc, page_index)

@spec text_lines!(t(), non_neg_integer()) :: [PdfElixide.Document.TextLine.t()]

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

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.

words(document)

@spec words(t()) ::
  {:ok, [PdfElixide.Document.Word.t()]} | {:error, PdfElixide.Error.t()}

Extracts the words of the whole document.

Returns every page's words concatenated into a single flat list, in page order. Each word carries its bounding box and font metadata as a PdfElixide.Document.Word struct.

words(document, page_index)

@spec words(t(), non_neg_integer()) ::
  {:ok, [PdfElixide.Document.Word.t()]} | {:error, PdfElixide.Error.t()}

Extracts the words of the page at the given zero-based index.

Each word carries its bounding box and font metadata as a PdfElixide.Document.Word struct.

words!(doc)

@spec words!(t()) :: [PdfElixide.Document.Word.t()]

Extracts the words of the whole document, raising an error if it fails.

words!(doc, page_index)

@spec words!(t(), non_neg_integer()) :: [PdfElixide.Document.Word.t()]

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