class Slim2pdf::Writer
Attributes
data[RW]
logger[W]
template[RW]
wkhtmltopdf_command[W]
wkhtmltopdf_path[RW]
Public Class Methods
new(template = nil, data = {})
click to toggle source
# File lib/slim2pdf/writer.rb, line 9 def initialize(template = nil, data = {}) @template = template @data = data @wkhtmltopdf_path = nil @wkhtmltopdf_command = nil end
Public Instance Methods
logger()
click to toggle source
# File lib/slim2pdf/writer.rb, line 42 def logger @logger ||= defined?(Rails) ? Rails.logger : nil rescue NoMethodError nil end
render_to_html()
click to toggle source
# File lib/slim2pdf/writer.rb, line 16 def render_to_html Slim::Template.new(template).render(scope) end
save_to_html(path)
click to toggle source
# 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)
click to toggle source
# 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
wkhtmltopdf_command()
click to toggle source
wkhtmltopdf command without html and pdf file params
# File lib/slim2pdf/writer.rb, line 37 def wkhtmltopdf_command path = @wkhtmltopdf_path || 'wkhtmltopdf' @wkhtmltopdf_command || "#{path} #{footer_params} -q" end
Private Instance Methods
create_dir(path)
click to toggle source
Create dir if not exists
# File lib/slim2pdf/writer.rb, line 56 def create_dir(path) FileUtils.mkdir_p(Pathname.new(path).dirname) end
create_tmp_html()
click to toggle source
# File lib/slim2pdf/writer.rb, line 60 def create_tmp_html tmp = Tempfile.new ['slim2pdf', '.html'] begin tmp.write render_to_html ensure tmp.close end tmp end
scope()
click to toggle source
Change hash data to scope object
# File lib/slim2pdf/writer.rb, line 51 def scope OpenStruct.new(data) end