PaperForge (PaperForge v1.4.3)

Copy Markdown View Source

Document engineering for the BEAM.

PaperForge provides the public API for building, securing, validating, and transforming native PDF documents entirely in Elixir. It includes functional document construction, measured layout, exact page drawing, metadata, serialization, and production output workflows.

Example

alias PaperForge.Page

document =
  PaperForge.new(compress: true)
  |> PaperForge.metadata(
    title: "PaperForge example",
    author: "Manuel García"
  )
  |> PaperForge.add_page(
    [
      size: :a4,
      origin: :top_left,
      margins: 72
    ],
    fn page ->
      page
      |> Page.text(
        "Hello PaperForge",
        y: 72,
        width: Page.content_width(page),
        align: :center,
        font: :helvetica_bold,
        size: 24
      )
      |> Page.text_box(
        "PaperForge generates PDF documents directly in Elixir.",
        y: 130,
        width: Page.content_width(page),
        font: :helvetica,
        size: 12,
        line_height: 16
      )
    end
  )

PaperForge.write!(document, "example.pdf")

Summary

Functions

Adds vertically flowed text blocks across as many pages as needed.

Adds a page to a document.

Creates a page with the provided options and adds it to the document.

Embeds a document attachment.

Applies structural PDF/A and PDF/UA conformance profiles.

Returns a structured debug report for a document.

Sets the default font key used by text operations that omit :font.

Returns the stable SHA-256 fingerprint of a document.

Renders a unified document flow.

Registers fallback embedded fonts for a primary font.

Returns a structured inventory suitable for diagnostics and editor tooling.

Renders a unified document flow and returns a layout report.

Adds vertically flowed text blocks and returns a layout report.

Adds metadata to the document.

Creates a new empty PDF document.

Registers a reusable page template.

Applies watermarks, tamper-evident metadata, and resource policies.

Registers an embedded TrueType font.

Registers a TrueType font family.

Renders a PDF and returns timing, memory, resource, and output diagnostics.

Registers a named style for headings, paragraphs, tables, and other flow blocks.

Serializes a document into a complete PDF binary.

Validates document structure and returns a deterministic validation report.

Validates document structure and raises PaperForge.ValidationError on failure.

Writes a document to a file.

Writes a document to a file and raises when writing fails.

Functions

add_flow(document, blocks, page_options \\ [], options \\ [])

Adds vertically flowed text blocks across as many pages as needed.

The flow uses top-left page coordinates so :y moves downward as content is added.

add_page(document, page_or_function)

Adds a page to a document.

The second argument can be an existing PaperForge.Page or a function that receives and returns a page.

add_page(document, page_options, page_function)

Creates a page with the provided options and adds it to the document.

Supported page options include:

  • :size
  • :orientation
  • :origin
  • :margins

add_table(document, rows, page_options \\ [], options \\ [])

@spec add_table(PaperForge.Document.t(), [[term()]], keyword(), keyword()) ::
  PaperForge.Document.t()

Adds a table across pages.

When :repeat_header is true, the first :header_rows rows are repeated at the top of every generated page. Rows are currently kept together; pass row_split: :keep.

attach(document, filename, data, options \\ [])

Embeds a document attachment.

comply(document, options)

Applies structural PDF/A and PDF/UA conformance profiles.

component(document, component_name, renderer)

Registers a reusable PaperForge.Flow component.

debug(document, options \\ [])

@spec debug(
  PaperForge.Document.t(),
  keyword()
) :: map()

Returns a structured debug report for a document.

default_font(document, font_key)

@spec default_font(PaperForge.Document.t(), atom()) :: PaperForge.Document.t()

Sets the default font key used by text operations that omit :font.

fingerprint(document)

@spec fingerprint(PaperForge.Document.t()) :: binary()

Returns the stable SHA-256 fingerprint of a document.

flow(document, flow_function)

Renders a unified document flow.

font_fallback(document, primary_font, fallbacks)

@spec font_fallback(PaperForge.Document.t(), atom(), [atom()]) ::
  PaperForge.Document.t()

Registers fallback embedded fonts for a primary font.

inspect_document(document)

@spec inspect_document(PaperForge.Document.t()) :: map()

Returns a structured inventory suitable for diagnostics and editor tooling.

layout(document, flow_function, options \\ [])

Renders a unified document flow and returns a layout report.

layout_flow(document, blocks, page_options \\ [], options \\ [])

@spec layout_flow(PaperForge.Document.t(), [iodata()], keyword(), keyword()) ::
  {PaperForge.Document.t(), map()}

Adds vertically flowed text blocks and returns a layout report.

metadata(document, options)

Adds metadata to the document.

new(options \\ [])

@spec new(keyword()) :: PaperForge.Document.t()

Creates a new empty PDF document.

Options

  • :compress — enables Flate compression for page content streams. Defaults to true.
  • :pdf_version — PDF header version. Defaults to "1.7".

page_template(document, template_name, options)

@spec page_template(PaperForge.Document.t(), atom() | binary(), keyword()) ::
  PaperForge.Document.t()

Registers a reusable page template.

protect(document, options \\ [])

Applies watermarks, tamper-evident metadata, and resource policies.

register_font(document, font_key, options)

@spec register_font(PaperForge.Document.t(), atom(), keyword()) ::
  PaperForge.Document.t()

Registers an embedded TrueType font.

Options

  • :path — path to a .ttf file.
  • :data — TrueType font binary.

register_font_family(document, family_key, variants)

@spec register_font_family(PaperForge.Document.t(), atom(), keyword()) ::
  PaperForge.Document.t()

Registers a TrueType font family.

Variants may include :regular, :bold, :italic, and :bold_italic. Each variant accepts the same options as register_font/3.

render(document, options \\ [])

@spec render(
  PaperForge.Document.t(),
  keyword()
) :: {:ok, binary(), map()}

Renders a PDF and returns timing, memory, resource, and output diagnostics.

style(document, style_name, options)

Registers a named style for headings, paragraphs, tables, and other flow blocks.

to_binary(document, options \\ [])

@spec to_binary(
  PaperForge.Document.t(),
  keyword()
) :: binary()

Serializes a document into a complete PDF binary.

validate(document)

@spec validate(PaperForge.Document.t()) ::
  {:ok, PaperForge.ValidationResult.t()} | {:error, [map()]}

Validates document structure and returns a deterministic validation report.

validate!(document)

@spec validate!(PaperForge.Document.t()) :: map()

Validates document structure and raises PaperForge.ValidationError on failure.

write(document, path, options \\ [])

@spec write(PaperForge.Document.t(), Path.t(), keyword()) ::
  :ok | {:error, File.posix()}

Writes a document to a file.

write!(document, path, options \\ [])

@spec write!(PaperForge.Document.t(), Path.t(), keyword()) :: :ok

Writes a document to a file and raises when writing fails.