Class: Slim2pdf::Writer
- Inherits:
-
Object
- Object
- Slim2pdf::Writer
- Defined in:
- lib/slim2pdf/writer.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#footer_font ⇒ Object
Returns the value of attribute footer_font.
-
#footer_font_size ⇒ Object
Returns the value of attribute footer_font_size.
-
#footer_text ⇒ Object
Returns the value of attribute footer_text.
- #logger ⇒ Object
-
#template ⇒ Object
Returns the value of attribute template.
-
#wkhtmltopdf_command ⇒ Object
wkhtmltopdf command without html and pdf file params.
-
#wkhtmltopdf_path ⇒ Object
Returns the value of attribute wkhtmltopdf_path.
Instance Method Summary collapse
-
#initialize(template = nil, data = {}) ⇒ Writer
constructor
A new instance of Writer.
- #render_to_html ⇒ Object
- #save_to_html(path) ⇒ Object
- #save_to_pdf(path) ⇒ Object
Constructor Details
#initialize(template = nil, data = {}) ⇒ Writer
Returns a new instance of Writer.
9 10 11 12 13 14 |
# File 'lib/slim2pdf/writer.rb', line 9 def initialize(template = nil, data = {}) @template = template @data = data @wkhtmltopdf_path = nil @wkhtmltopdf_command = nil end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
3 4 5 |
# File 'lib/slim2pdf/writer.rb', line 3 def data @data end |
#footer_font ⇒ Object
Returns the value of attribute footer_font.
6 7 8 |
# File 'lib/slim2pdf/writer.rb', line 6 def @footer_font end |
#footer_font_size ⇒ Object
Returns the value of attribute footer_font_size.
6 7 8 |
# File 'lib/slim2pdf/writer.rb', line 6 def @footer_font_size end |
#footer_text ⇒ Object
Returns the value of attribute footer_text.
6 7 8 |
# File 'lib/slim2pdf/writer.rb', line 6 def @footer_text end |
#logger ⇒ Object
42 43 44 45 46 |
# File 'lib/slim2pdf/writer.rb', line 42 def logger @logger ||= defined?(Rails) ? Rails.logger : nil rescue NoMethodError nil end |
#template ⇒ Object
Returns the value of attribute template.
3 4 5 |
# File 'lib/slim2pdf/writer.rb', line 3 def template @template end |
#wkhtmltopdf_command ⇒ Object
wkhtmltopdf command without html and pdf file params
37 38 39 40 |
# File 'lib/slim2pdf/writer.rb', line 37 def wkhtmltopdf_command path = @wkhtmltopdf_path || 'wkhtmltopdf' @wkhtmltopdf_command || "#{path} #{} -q" end |
#wkhtmltopdf_path ⇒ Object
Returns the value of attribute wkhtmltopdf_path.
4 5 6 |
# File 'lib/slim2pdf/writer.rb', line 4 def wkhtmltopdf_path @wkhtmltopdf_path end |
Instance Method Details
#render_to_html ⇒ Object
16 17 18 |
# File 'lib/slim2pdf/writer.rb', line 16 def render_to_html Slim::Template.new(template).render(scope) end |
#save_to_html(path) ⇒ Object
20 21 22 23 24 |
# File 'lib/slim2pdf/writer.rb', line 20 def save_to_html(path) create_dir(path) File.write(path, render_to_html) logger.info "[Slim2pdf] HTML file saved: #{path}" if logger end |
#save_to_pdf(path) ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/slim2pdf/writer.rb', line 26 def save_to_pdf(path) create_dir(path) html = create_tmp_html full_command = "#{wkhtmltopdf_command} #{html.path} #{path}" logger.debug "[Slim2pdf] Run command: #{full_command}" if logger `#{full_command}` html.unlink logger.info "[Slim2pdf] PDF file saved: #{path}" if logger end |