Module: WeasyPDF::PdfHelper

Defined in:
lib/weasy_pdf/pdf_helper.rb

Constant Summary collapse

RAILS_RENDER_KEYS =
%i[
  template layout file inline locals formats handlers assigns
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/weasy_pdf/pdf_helper.rb', line 5

def self.prepended(base)
  # Guard prevents after_action from being registered twice when PdfHelper
  # is prepended to both ActionController::Base and a concrete subclass.
  return unless base == ActionController::Base

  base.class_eval do
    after_action :clean_temp_files
  end
end

Instance Method Details

#render(*args) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/weasy_pdf/pdf_helper.rb', line 15

def render(*args)
  if args.first.is_a?(Hash) && args.first.key?(:pdf)
    render_with_weasy_pdf(args.first)
  else
    super
  end
end

#render_to_string(*args) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/weasy_pdf/pdf_helper.rb', line 23

def render_to_string(*args)
  if args.first.is_a?(Hash) && args.first.key?(:pdf)
    render_to_string_with_weasy_pdf(args.first)
  else
    super
  end
end

#render_to_string_with_weasy_pdf(options) ⇒ Object



52
53
54
# File 'lib/weasy_pdf/pdf_helper.rb', line 52

def render_to_string_with_weasy_pdf(options)
  make_pdf(options.except(:pdf, :disposition, :show_as_html))
end

#render_with_weasy_pdf(options) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/weasy_pdf/pdf_helper.rb', line 35

def render_with_weasy_pdf(options)
  options = options.dup

  filename = options.delete(:pdf) || "document"
  show_as_html = options.delete(:show_as_html)
  disposition = options.delete(:disposition) || "inline"

  return render(options.slice(*RAILS_RENDER_KEYS).merge(content_type: "text/html")) if show_as_html

  pdf = make_pdf(options)

  send_data pdf,
    filename: "#{filename}.pdf",
    type: "application/pdf",
    disposition: disposition
end