PaperForge (PaperForge v0.3.0)

Copy Markdown View Source

Public API for creating PDF documents with PaperForge.

PaperForge provides a functional API for creating documents, adding pages, assigning metadata, serializing PDFs, and writing them to disk.

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.

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

Adds metadata to the document.

Creates a new empty PDF document.

Registers an embedded TrueType font.

Registers a TrueType font family.

Serializes a document into a complete PDF binary.

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

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.

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".

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.

to_binary(document)

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

Serializes a document into a complete PDF binary.

write(document, path)

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

Writes a document to a file.

write!(document, path)

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

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