Module: Typstify::Renderer

Defined in:
lib/typstify/renderer.rb

Overview

Registers render pdf: on every controller.

render pdf: "invoices/show",
     data: { number: "A-1" },
     filename: "invoice-A-1.pdf",
     disposition: :inline

filename defaults to the template's basename; disposition to attachment, matching send_data.

Constant Summary collapse

DEFAULT_DISPOSITION =
:attachment

Class Method Summary collapse

Class Method Details

.filename_for(template, options) ⇒ Object



36
37
38
39
40
41
# File 'lib/typstify/renderer.rb', line 36

def self.filename_for(template, options)
  return options[:filename] if options[:filename]

  base = File.basename(template.to_s).sub(/\.typ(\.erb)?\z/, "")
  "#{base}.pdf"
end

.install!Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/typstify/renderer.rb', line 18

def self.install!
  ActionController::Renderers.add :pdf do |template, options|
    pdf = Typstify.render(
      template: template,
      data: options[:data],
      assigns: view_assigns
    )

    send_data(
      pdf,
      type: options[:type] || "application/pdf",
      filename: Typstify::Renderer.filename_for(template, options),
      disposition: options[:disposition] || DEFAULT_DISPOSITION,
      status: options[:status] || :ok
    )
  end
end