Typst (Typst v0.2.2)

View Source

This module provides the core functions for interacting with the typst markup language compiler.

Note that when using the formatting directives, they are exactly the same as EEx, so all of its constructs are supported.

See Typst's documentation for a quickstart.

Summary

Functions

Converts a given piece of typst markup to a PDF binary.

Converts a given piece of typst markup to a PNG binary, one per each page. #

Formats the given markup template with the given bindings, mostly useful for inspecting and debugging.

Types

formattable()

@type formattable() :: {atom(), any()}

typst_opt()

@type typst_opt() ::
  {:extra_fonts, [String.t()]}
  | {:root_dir, String.t()}
  | {:pixels_per_pt, number()}
  | {:assets, Keyword.t() | Map.t() | [{String.t(), binary()}]}

Functions

render_to_pdf(typst_markup, bindings \\ [], opts \\ [])

@spec render_to_pdf(String.t(), [formattable()], [typst_opt()]) ::
  {:ok, binary()} | {:error, String.t()}

Converts a given piece of typst markup to a PDF binary.

Options

This function takes the following options:

  • :extra_fonts - a list of directories to seatch for fonts

  • :root_dir - the root directory for typst, where all filepaths are resolved from. defaults to the current directory

  • :assets - a list of {"name", binary()} or enumerable to store blobs in the typst virtual file system

Examples

iex> {:ok, pdf} = Typst.render_to_pdf("= test\n<%= name %>", name: "John")
iex> is_binary(pdf)
true

iex> svg = ~S|<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="m19.5 8.25-7.5 7.5-7.5-7.5" /></svg>|
iex> {:ok, pdf} = Typst.render_to_pdf(~S|#image(read("logo", encoding: none), width: 6cm)|, [], assets: [logo: svg])
iex> is_binary(pdf)
true

render_to_pdf!(typst_markup, bindings \\ [], opts \\ [])

@spec render_to_pdf!(String.t(), [formattable()], [typst_opt()]) :: binary()

Same as render_to_pdf/3, but raises if the rendering fails.

render_to_png(typst_markup, bindings \\ [], opts \\ [])

@spec render_to_png(String.t(), [formattable()], [typst_opt()]) ::
  {:ok, [binary()]} | {:error, String.t()}

Converts a given piece of typst markup to a PNG binary, one per each page. #

Options

This function takes the following options:

  • :extra_fonts - a list of directories to seatch for fonts

  • :root_dir - the root directory for typst, where all filepaths are resolved from. defaults to the current directory

  • :pixels_per_pt - specifies how many pixels represent one pt unit

  • :assets - a list of {"name", binary()} or enumerable to store blobs in the typst virtual file system

Examples

iex> {:ok, pngs} = Typst.render_to_png("= test\n<%= name %>", name: "John")
iex> is_list(pngs)
true

render_to_png!(typst_markup, bindings \\ [], opts \\ [])

@spec render_to_png!(String.t(), [formattable()], [typst_opt()]) :: [binary()]

Same as render_to_png/3, but raises if the rendering fails.

render_to_string(typst_markup, bindings \\ [])

@spec render_to_string(String.t(), [formattable()]) :: String.t()

Formats the given markup template with the given bindings, mostly useful for inspecting and debugging.

Examples

iex> Typst.render_to_string("= Hey <%= name %>!", name: "Jude")
"= Hey Jude!"