module WickedPdf::PdfHelper
Public Class Methods
prepended(base)
click to toggle source
# File lib/wicked_pdf/pdf_helper.rb, line 3 def self.prepended(base) # Protect from trying to augment modules that appear # as the result of adding other gems. return if base != ActionController::Base base.class_eval do after_action :clean_temp_files end end
Public Instance Methods
render(*args)
click to toggle source
Calls superclass method
# File lib/wicked_pdf/pdf_helper.rb, line 13 def render(*args) options = args.first if options.is_a?(Hash) && options.key?(:pdf) render_with_wicked_pdf(options) else super end end
render_to_string(*args)
click to toggle source
Calls superclass method
# File lib/wicked_pdf/pdf_helper.rb, line 22 def render_to_string(*args) options = args.first if options.is_a?(Hash) && options.key?(:pdf) render_to_string_with_wicked_pdf(options) else super end end
render_to_string_with_wicked_pdf(options)
click to toggle source
# File lib/wicked_pdf/pdf_helper.rb, line 38 def render_to_string_with_wicked_pdf(options) raise ArgumentError, 'missing keyword: pdf' unless options.is_a?(Hash) && options.key?(:pdf) options[:basic_auth] = set_basic_auth(options) options.delete :pdf make_pdf((WickedPdf.config || {}).merge(options)) end
render_with_wicked_pdf(options)
click to toggle source
# File lib/wicked_pdf/pdf_helper.rb, line 31 def render_with_wicked_pdf(options) raise ArgumentError, 'missing keyword: pdf' unless options.is_a?(Hash) && options.key?(:pdf) options[:basic_auth] = set_basic_auth(options) make_and_send_pdf(options.delete(:pdf), (WickedPdf.config || {}).merge(options)) end
Private Instance Methods
clean_temp_files()
click to toggle source
# File lib/wicked_pdf/pdf_helper.rb, line 55 def clean_temp_files return unless defined?(@hf_tempfiles) @hf_tempfiles.each(&:close) end
make_and_send_pdf(pdf_name, options = {})
click to toggle source
# File lib/wicked_pdf/pdf_helper.rb, line 78 def make_and_send_pdf(pdf_name, options = {}) options[:wkhtmltopdf] ||= nil options[:layout] ||= false options[:template] ||= File.join(controller_path, action_name) options[:disposition] ||= 'inline' if options[:show_as_html] render_opts = { :template => options[:template], :layout => options[:layout], :formats => options[:formats], :handlers => options[:handlers], :assigns => options[:assigns], :content_type => 'text/html' } render_opts[:inline] = options[:inline] if options[:inline] render_opts[:locals] = options[:locals] if options[:locals] render_opts[:file] = options[:file] if options[:file] render(render_opts) else pdf_content = make_pdf(options) File.open(options[:save_to_file], 'wb') { |file| file << pdf_content } if options[:save_to_file] send_data(pdf_content, :filename => pdf_name + '.pdf', :type => 'application/pdf', :disposition => options[:disposition]) unless options[:save_only] end end
make_pdf(options = {})
click to toggle source
# File lib/wicked_pdf/pdf_helper.rb, line 61 def make_pdf(options = {}) render_opts = { :template => options[:template], :layout => options[:layout], :formats => options[:formats], :handlers => options[:handlers], :assigns => options[:assigns] } render_opts[:inline] = options[:inline] if options[:inline] render_opts[:locals] = options[:locals] if options[:locals] render_opts[:file] = options[:file] if options[:file] html_string = render_to_string(render_opts) options = prerender_header_and_footer(options) w = WickedPdf.new(options[:wkhtmltopdf]) w.pdf_from_string(html_string, options) end
set_basic_auth(options = {})
click to toggle source
# File lib/wicked_pdf/pdf_helper.rb, line 48 def set_basic_auth(options = {}) options[:basic_auth] ||= WickedPdf.config.fetch(:basic_auth) { false } return unless options[:basic_auth] && request.env['HTTP_AUTHORIZATION'] request.env['HTTP_AUTHORIZATION'].split(' ').last end