Class: Slim2pdf::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/slim2pdf/writer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#dataObject

Returns the value of attribute data.



3
4
5
# File 'lib/slim2pdf/writer.rb', line 3

def data
  @data
end

Returns the value of attribute footer_font.



6
7
8
# File 'lib/slim2pdf/writer.rb', line 6

def footer_font
  @footer_font
end

Returns the value of attribute footer_font_size.



6
7
8
# File 'lib/slim2pdf/writer.rb', line 6

def footer_font_size
  @footer_font_size
end

Returns the value of attribute footer_text.



6
7
8
# File 'lib/slim2pdf/writer.rb', line 6

def footer_text
  @footer_text
end

#loggerObject



42
43
44
45
46
# File 'lib/slim2pdf/writer.rb', line 42

def logger
  @logger ||= defined?(Rails) ? Rails.logger : nil
rescue NoMethodError
  nil
end

#templateObject

Returns the value of attribute template.



3
4
5
# File 'lib/slim2pdf/writer.rb', line 3

def template
  @template
end

#wkhtmltopdf_commandObject

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} #{footer_params} -q"
end

#wkhtmltopdf_pathObject

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_htmlObject



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