9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/docmago_client/railtie.rb', line 9
def self.setup_actioncontroller
unless Mime::Type.lookup_by_extension("pdf")
Mime::Type.register "application/pdf", :pdf
end
ActionController::Renderers.add :pdf do |filename, options|
append_view_path ActionView::FileSystemResolver.new(
Rails.root.join("app/views"),
":prefix/{:formats/,}:action{.:locale,}{.:formats,}{.:handlers,}"
)
default_options = {
name: filename||controller_name,
test_mode: !Rails.env.production?,
base_uri: url_for(only_path: false),
resource_path: Rails.root.join('public').to_s,
zip_resources: true
}
options = default_options.merge(options)
options[:content] ||= render_to_string(options)
response = DocmagoClient.create(options)
if response.code == 200
send_data response, filename: "#{options[:name]}.pdf", type: "application/pdf", disposition: "attachment"
else
render inline: response.body, status: response.code
end
end
end
|