Sha256: 9c731dfbccfa6eb0730bca146abb6cfda13be871af204d232e5f87f2b575d6bc

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

module Slim2pdf
  class Writer
    attr_accessor :template, :data
    attr_accessor :footer_text, :footer_font, :footer_font_size

    def initialize(template=nil, data={})
      @template = template
      @data = data
    end

    def render_to_html
      Slim::Template.new(template).render(scope)
    end

    def save_to_html(path)
      create_dir(path)
      File.write(path, render_to_html)
    end

    def save_to_pdf(path)
      create_dir(path)
      html = create_tmp_html
      `#{wkhtmltopdf_command(html.path, path)}`
      html.unlink
    end

    def wkhtmltopdf_command(html_path, pdf_path)
      "wkhtmltopdf #{footer_params} -q #{html_path} #{pdf_path}"
    end

    private
      # Change hash data to scope object
      def scope
        OpenStruct.new(data)
      end

      # Create dir if not exists
      def create_dir(path)
        FileUtils.mkdir_p(Pathname.new(path).dirname)
      end

      def create_tmp_html
        tmp = Tempfile.new ['slim2pdf', '.html']
        begin
          tmp.write render_to_html
        ensure
          tmp.close
        end
        tmp
      end

      def footer_params
        return unless footer_text
        "--footer-line --footer-font-name '#{footer_font || 'verdana'}' " +
        "--footer-font-size '#{footer_font_size || 10}' --footer-left '#{footer_text}'"
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
slim2pdf-0.0.1 lib/slim2pdf/writer.rb