PrawnEx.Document (prawn_ex v0.4.0)

Copy Markdown View Source

Document spec: immutable struct holding options and a list of pages.

Options:

  • :page_size - :a4, :letter, etc. (default :a4)
  • :margins - optional %{left: pt, right: pt, top: pt, bottom: pt}

The last page in pages is the "current" page for appending content. Images are stored in images (list of image specs) and referenced by 1-based index.

Summary

Functions

Appends an image spec to the document. Returns {doc, image_id} (image_id is 1-based).

Appends a new page and returns the updated document. The new page becomes current.

Appends an annotation (e.g. link) to the current page. Returns the updated document.

Appends a content op to the current page. If there is no page, adds one first. Returns the updated document.

Returns the current page (last in the list), or nil if no pages.

Injects ops at the start and/or end of a page's content. Used for headers/footers.

Creates a new document with optional options.

Registers an embedded TrueType font under name for use with set_font/3.

Types

opts()

@type opts() :: keyword()

t()

@type t() :: %PrawnEx.Document{
  fonts: %{optional(String.t()) => map()},
  images: [map()],
  opts: keyword(),
  pages: [PrawnEx.Page.t()]
}

Functions

add_image(doc, spec)

@spec add_image(t(), map()) :: {t(), pos_integer()}

Appends an image spec to the document. Returns {doc, image_id} (image_id is 1-based).

add_page(doc)

@spec add_page(t()) :: t()

Appends a new page and returns the updated document. The new page becomes current.

append_annotation(doc, annot)

@spec append_annotation(t(), map()) :: t()

Appends an annotation (e.g. link) to the current page. Returns the updated document.

append_op(doc, op)

@spec append_op(t(), PrawnEx.Page.content_op()) :: t()

Appends a content op to the current page. If there is no page, adds one first. Returns the updated document.

current_page(document)

@spec current_page(t()) :: PrawnEx.Page.t() | nil

Returns the current page (last in the list), or nil if no pages.

inject_page_ops(doc, page_index, prepend_ops, append_ops)

@spec inject_page_ops(t(), non_neg_integer(), [PrawnEx.Page.content_op()], [
  PrawnEx.Page.content_op()
]) ::
  t()

Injects ops at the start and/or end of a page's content. Used for headers/footers.

page_index is 0-based. Returns the document with that page's content_ops set to prepend_ops ++ current_ops ++ append_ops.

new(opts \\ [])

@spec new(opts()) :: t()

Creates a new document with optional options.

Examples

iex> doc = PrawnEx.Document.new()
iex> doc.pages
[]
iex> doc = PrawnEx.Document.new(page_size: :letter)
iex> doc.opts[:page_size]
:letter

put_font(doc, name, data, metrics)

@spec put_font(t(), String.t(), binary(), map()) :: t()

Registers an embedded TrueType font under name for use with set_font/3.