module RenderExpand
  extend ActiveSupport::Concern

  included do
    ActionController.add_renderer :pdf do |template, options|
      file = File.open(Rails.root.join('app/templates', template << '.html.erb'))
      html = ERB.new(file.read).result(binding)
      kit = PDFKit.new(html)

      base_css = %w(app/templates/shared/main.css)
      base_css.each { |css| kit.stylesheets << Rails.root.join(css) }

      Array.wrap(options.delete(:stylesheets)).each do |path|
        kit.stylesheets << Rails.root.join('app/templates', path)
      end

      send_data kit.to_pdf, filename: options[:filename], disposition: options[:disposition] || 'attachment', type: 'application/pdf'
    end
  end
end