Module: Typstify

Defined in:
lib/typstify.rb,
lib/typstify/data.rb,
lib/typstify/fonts.rb,
lib/typstify/config.rb,
lib/typstify/engine.rb,
lib/typstify/errors.rb,
lib/typstify/adapter.rb,
lib/typstify/version.rb,
lib/typstify/document.rb,
lib/typstify/escaping.rb,
lib/typstify/renderer.rb,
lib/typstify/resolver.rb,
lib/typstify/warnings.rb,
lib/typstify/workspace.rb,
lib/typstify/erb_pipeline.rb,
lib/generators/typstify/install/install_generator.rb,
lib/generators/typstify/template/template_generator.rb

Overview

PDF generation for Rails on the Typst engine.

Typstify.render(template: "invoices/show", data: { number: "A-1" })

In a controller, prefer the renderer:

render pdf: "invoices/show", data: { ... }, filename: "invoice.pdf"

Defined Under Namespace

Modules: Adapter, Data, ErbPipeline, Escaping, Fonts, Generators, Renderer, Warnings Classes: CompileError, Config, Document, Engine, Error, FontMissingError, MissingTemplate, PathError, Resolver, Workspace

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.apply_package_cache!Object

Point Typst's package resolution at the configured cache.

Typst looks in the platform data directory, which on Linux — and so in every Docker image — is XDG_DATA_HOME. Set once at boot rather than around each compile, because mutating ENV per render is not thread-safe and concurrent rendering is a supported use. On macOS the platform directory is fixed and this has no effect; see docs/fonts-and-docker.md.



97
98
99
100
101
102
# File 'lib/typstify.rb', line 97

def apply_package_cache!
  cache = config.package_cache
  return if cache.nil?

  ENV["XDG_DATA_HOME"] = cache.expand_path.to_s
end

.bundled_font_pathObject

Faces shipped with the gem, always on the font search path so the starter templates render with no configuration.



81
82
83
# File 'lib/typstify.rb', line 81

def bundled_font_path
  root.join("fonts")
end

.configObject



30
31
32
# File 'lib/typstify.rb', line 30

def config
  @config ||= Config.new
end

.configure {|config| ... } ⇒ Object

Yields:



34
35
36
37
38
# File 'lib/typstify.rb', line 34

def configure
  yield config
  apply_package_cache!
  config
end

.render(template:, data: nil, assigns: {}) ⇒ String

Compile a template to PDF.

Parameters:

  • template (String)

    e.g. "invoices/show"

  • data (Object) (defaults to: nil)

    anything JSON-serializable; read in the template with #let data = json("data.json")

  • assigns (Hash) (defaults to: {})

    instance variables for ERB mode only

Returns:

  • (String)

    PDF bytes, ASCII-8BIT



54
55
56
# File 'lib/typstify.rb', line 54

def render(template:, data: nil, assigns: {})
  Document.new(template: template, data: data, assigns: assigns, config: config).to_pdf
end

.render_and_attach(attachable, template:, filename:, data: nil, assigns: {}, content_type: "application/pdf") ⇒ Object

Render and attach in one step.

Typstify.render_and_attach(user.documents,
                         template: "certificates/completion",
                         data: cert_data, filename: "certificate.pdf")

Parameters:

  • attachable (#attach)

    an ActiveStorage has_one/has_many attachment proxy



65
66
67
68
69
70
71
72
73
# File 'lib/typstify.rb', line 65

def render_and_attach(attachable, template:, filename:, data: nil, assigns: {},
                      content_type: "application/pdf")
  pdf = render(template: template, data: data, assigns: assigns)
  attachable.attach(
    io: StringIO.new(pdf),
    filename: filename,
    content_type: content_type
  )
end

.reset!Object

Mostly for specs: drop all configuration and cached font scans.



41
42
43
44
45
# File 'lib/typstify.rb', line 41

def reset!
  @config = Config.new
  Fonts.reset!
  config
end

.rootObject



75
76
77
# File 'lib/typstify.rb', line 75

def root
  @root ||= Pathname.new(__dir__).join("..").expand_path
end

.template_pack_pathObject

The starter template pack, copied by rails g typstify:template.



86
87
88
# File 'lib/typstify.rb', line 86

def template_pack_path
  root.join("templates")
end