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.
Adds a table across pages.
Embeds a document attachment.
Registers a reusable PaperForge.Flow component.
Returns a structured debug report for a document.
Sets the default font key used by text operations that omit :font.
Renders a unified document flow.
Registers fallback embedded fonts for a primary font.
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.
Registers an embedded TrueType font.
Registers a TrueType font family.
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
@spec add_flow(PaperForge.Document.t(), [iodata()], keyword(), keyword()) :: PaperForge.Document.t()
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.
@spec add_page( PaperForge.Document.t(), PaperForge.Page.t() | (PaperForge.Page.t() -> PaperForge.Page.t()) ) :: PaperForge.Document.t()
Adds a page to a document.
The second argument can be an existing PaperForge.Page or a function
that receives and returns a page.
@spec add_page( PaperForge.Document.t(), keyword(), (PaperForge.Page.t() -> PaperForge.Page.t()) ) :: PaperForge.Document.t()
Creates a page with the provided options and adds it to the document.
Supported page options include:
:size:orientation:origin:margins
@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.
@spec attach(PaperForge.Document.t(), binary(), binary(), keyword()) :: PaperForge.Document.t()
Embeds a document attachment.
@spec component(PaperForge.Document.t(), atom(), (map() -> PaperForge.Flow.t())) :: PaperForge.Document.t()
Registers a reusable PaperForge.Flow component.
@spec debug( PaperForge.Document.t(), keyword() ) :: map()
Returns a structured debug report for a document.
@spec default_font(PaperForge.Document.t(), atom()) :: PaperForge.Document.t()
Sets the default font key used by text operations that omit :font.
@spec flow( PaperForge.Document.t(), (PaperForge.Flow.t() -> PaperForge.Flow.t()) | keyword() ) :: PaperForge.Document.t()
Renders a unified document flow.
@spec font_fallback(PaperForge.Document.t(), atom(), [atom()]) :: PaperForge.Document.t()
Registers fallback embedded fonts for a primary font.
@spec layout( PaperForge.Document.t(), (PaperForge.Flow.t() -> PaperForge.Flow.t()), keyword() ) :: {PaperForge.Document.t(), map()}
Renders a unified document flow and returns a layout report.
@spec layout_flow(PaperForge.Document.t(), [iodata()], keyword(), keyword()) :: {PaperForge.Document.t(), map()}
Adds vertically flowed text blocks and returns a layout report.
@spec metadata( PaperForge.Document.t(), keyword() ) :: PaperForge.Document.t()
Adds metadata to the document.
@spec new(keyword()) :: PaperForge.Document.t()
Creates a new empty PDF document.
Options
:compress— enables Flate compression for page content streams. Defaults totrue.:pdf_version— PDF header version. Defaults to"1.7".
@spec page_template(PaperForge.Document.t(), atom(), keyword()) :: PaperForge.Document.t()
Registers a reusable page template.
@spec register_font(PaperForge.Document.t(), atom(), keyword()) :: PaperForge.Document.t()
Registers an embedded TrueType font.
Options
:path— path to a.ttffile.:data— TrueType font binary.
@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.
@spec style(PaperForge.Document.t(), atom(), keyword()) :: PaperForge.Document.t()
Registers a named style for headings, paragraphs, tables, and other flow blocks.
@spec to_binary(PaperForge.Document.t()) :: binary()
Serializes a document into a complete PDF binary.
@spec validate(PaperForge.Document.t()) :: {:ok, map()} | {:error, [map()]}
Validates document structure and returns a deterministic validation report.
@spec validate!(PaperForge.Document.t()) :: map()
Validates document structure and raises PaperForge.ValidationError on failure.
@spec write(PaperForge.Document.t(), Path.t()) :: :ok | {:error, File.posix()}
Writes a document to a file.
@spec write!(PaperForge.Document.t(), Path.t()) :: :ok
Writes a document to a file and raises when writing fails.