Module: HtmlToPdf
- Defined in:
- lib/htmlToPdf.rb,
lib/htmlToPdf/version.rb,
lib/htmlToPdf/configuration.rb
Defined Under Namespace
Classes: Configuration
Constant Summary collapse
- FALSEY_VALUES =
Query-string values arrive as strings (e.g. grayscale: true serializes to "true" in a link_to href and comes back as that literal string), so plain booleans aren't enough to detect a falsy/truthy flag.
[nil, false, '', '0', 'false', 'off'].freeze
- TRUTHY_FLAG_VALUES =
[true, 'true'].freeze
- VERSION =
"2.0.0"
Class Attribute Summary collapse
Class Method Summary collapse
Instance Method Summary collapse
- #build_wkhtmltopdf_options(pdf_options) ⇒ Object
- #dasherize_keys(hash) ⇒ Object
- #html_to_pdf ⇒ Object
- #initialize_downloads(pdf_options = {}) ⇒ Object
- #sanitized_pdf_name ⇒ Object
- #wkhtmltopdf_args ⇒ Object
Class Attribute Details
.configuration ⇒ Object
16 17 18 |
# File 'lib/htmlToPdf/configuration.rb', line 16 def configuration @configuration ||= Configuration.new end |
Class Method Details
.configure {|configuration| ... } ⇒ Object
20 21 22 |
# File 'lib/htmlToPdf/configuration.rb', line 20 def configure yield(configuration) end |
.reset_configuration! ⇒ Object
24 25 26 |
# File 'lib/htmlToPdf/configuration.rb', line 24 def reset_configuration! @configuration = Configuration.new end |
Instance Method Details
#build_wkhtmltopdf_options(pdf_options) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/htmlToPdf.rb', line 56 def () = dasherize_keys(HtmlToPdf.configuration.) if [:padding].present? padding = [:padding].to_s %w[margin-top margin-bottom margin-left margin-right].each { |flag| [flag] = padding } end .merge!(dasherize_keys([:wkhtmltopdf_options] || {})) end |
#dasherize_keys(hash) ⇒ Object
68 69 70 |
# File 'lib/htmlToPdf.rb', line 68 def dasherize_keys(hash) hash.each_with_object({}) { |(key, value), acc| acc[key.to_s.tr('_', '-')] = value } end |
#html_to_pdf ⇒ Object
7 8 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 |
# File 'lib/htmlToPdf.rb', line 7 def html_to_pdf() = params[:pdf_options] ? params[:pdf_options] : {} if request.format == 'application/pdf' initialize_downloads() _tmp_dir = HtmlToPdf.configuration.tmp_dir || File.join(Rails.root, 'tmp') FileUtils.mkdir_p(_tmp_dir) _unique_id = "#{Time.now.to_i}_#{SecureRandom.hex(8)}" _source_file = File.join(_tmp_dir, "#{_unique_id}.html") _destination_file = File.join(_tmp_dir, "#{_unique_id}.pdf") _controller = ("#{controller_name}Controller").camelize.constantize.new _controller.request = request _controller.response = response _controller.params = params _controller.method(action_name).call _controller.instance_variables.each do |var| instance_variable_set(var, _controller.instance_variable_get(var)) end _html = render_to_string(template: "#{controller_name}/#{action_name}", :layout => @layout, locals: { url: Rails.application.routes.url_helpers }) file_content = _html.gsub(/\\"/, "") File.open(_source_file, "w+") do |f| f.write(file_content) end system(HtmlToPdf.configuration.wkhtmltopdf_path, *wkhtmltopdf_args, _source_file, _destination_file) File.delete(_source_file) if File.exist?(_source_file) send_data File.open(_destination_file, "rb") { |f| f.read }, :disposition => 'attachment', :filename => "#{sanitized_pdf_name}.pdf" File.delete(_destination_file) end end |
#initialize_downloads(pdf_options = {}) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/htmlToPdf.rb', line 40 def initialize_downloads( = {}) ||= {} @name = [:title].nil? ? "#{Time.now.to_i.to_s}" : [:title] @layout = [:layout].nil? ? HtmlToPdf.configuration.default_layout : [:layout] @wkhtmltopdf_options = () end |
#sanitized_pdf_name ⇒ Object
47 48 49 |
# File 'lib/htmlToPdf.rb', line 47 def sanitized_pdf_name @name.to_s.gsub(/[^a-zA-Z0-9_\- ]/, '').strip.presence || Time.now.to_i.to_s end |
#wkhtmltopdf_args ⇒ Object
78 79 80 81 82 83 84 85 86 |
# File 'lib/htmlToPdf.rb', line 78 def wkhtmltopdf_args (@wkhtmltopdf_options || {}).each_with_object([]) do |(flag, value), args| normalized = value.is_a?(String) ? value.downcase : value next if FALSEY_VALUES.include?(normalized) args << "--#{flag}" args << value.to_s unless TRUTHY_FLAG_VALUES.include?(normalized) end end |