Imprintor (imprintor v0.7.0)

Copy Markdown View Source

Imprintor is a library for generating PDF and PNG documents from Typst templates.

It provides functions to compile Typst templates with data interpolation and generate PDF or PNG output using a native Rust implementation.

Summary

Functions

Wraps raw binary data to be consumed as Typst bytes.

Compiles a Typst template to a PDF document.

Compiles a Typst template to a PDF file.

Compiles a Typst template to one or more PNG images, one per page.

Compiles a Typst template to one or more PNG files, one per page.

Functions

bytes(binary)

Wraps raw binary data to be consumed as Typst bytes.

This is useful for APIs like pdf.attach that expect a bytes value.

Examples

data = %{
  "factur_x_xml" => Imprintor.bytes("<xml>...</xml>")
}

compile_to_pdf(config)

Compiles a Typst template to a PDF document.

Takes an Imprintor.Config struct containing the template configuration and returns a binary containing the compiled PDF data.

Parameters

  • config - An %Imprintor.Config{} struct containing:
    • Template source or file path
    • Data for interpolation
    • Compilation options

Returns

  • {:ok, pdf_binary} - Successfully compiled PDF as binary data
  • {:error, reason} - Compilation failed with error reason

compile_to_pdf_file(config, output_path)

Compiles a Typst template to a PDF file.

Takes an Imprintor.Config struct and an output file path, compiles the template, and writes the resulting PDF to the specified file.

Parameters

  • config - An %Imprintor.Config{} struct containing:
    • Template source or file path
    • Data for interpolation
    • Compilation options
  • output_path - A string specifying the file path to write the PDF to

compile_to_png(config)

Compiles a Typst template to one or more PNG images, one per page.

Takes an Imprintor.Config struct containing the template configuration and returns a list of binaries, each containing the compiled PNG data for one page, in page order.

Parameters

  • config - An %Imprintor.Config{} struct containing:
    • Template source or file path
    • Data for interpolation
    • Compilation options
    • :ppi - Pixels-per-inch used for rendering (defaults to 144.0)

Returns

  • {:ok, [png_binary, ...]} - Successfully compiled PNG(s) as binary data
  • {:error, reason} - Compilation failed with error reason

compile_to_png_file(config, output_path)

Compiles a Typst template to one or more PNG files, one per page.

Takes an Imprintor.Config struct and an output file path, compiles the template, and writes the resulting PNG(s) to disk.

For a single-page document, the image is written directly to output_path. For a multi-page document, the 1-based page number is inserted before the file extension for each page (e.g. report.png becomes report-1.png, report-2.png, ...).

Parameters

  • config - An %Imprintor.Config{} struct containing:
    • Template source or file path
    • Data for interpolation
    • Compilation options
    • :ppi - Pixels-per-inch used for rendering (defaults to 144.0)
  • output_path - A string specifying the file path to write the PNG(s) to

Returns

  • {:ok, [path, ...]} - Successfully wrote the PNG(s), in page order
  • {:error, reason} - Compilation or writing failed with error reason

typst_to_pdf(config)

typst_to_pdf_file(config, output_path)

typst_to_png(config)

typst_to_png_file(config, output_path)