Sha256: 008aea3e393aa95ae2d60d75bf97cf0bd9b1296ae8d70178848685bfb1842fa2

Contents?: true

Size: 1.01 KB

Versions: 2

Compression:

Stored size: 1.01 KB

Contents

module Ezprint
  module Processors
    class Prince < Base
      def self.process(html_string, options = {})
        pdf = IO.popen(self.cmd(options), "w+")
        pdf.puts(self.process_html(html_string))
        pdf.close_write
        result = pdf.gets(nil)
        pdf.close_read
        result
      end

      def self.cmd(options)
        stylesheets           = options.delete(:stylesheets)
        prince_cmd            = `which prince`.chomp

        if prince_cmd.length == 0
          raise RuntimeError.new "Cannot locate prince binary. Please check your path"
        end

        prince_cmd << " --input=html --server "
        stylesheets.each { |s| prince_cmd << " -s #{s} " }
        prince_cmd << " --silent - -o -"
      end

      def self.process_html(html)
        # reroute absolute paths
        html.gsub!("src=\"/", "src=\"#{Rails.public_path}/")
        html.gsub!("href=\"/", "src=\"#{Rails.public_path}/")
        html.gsub!("url(/", "url(#{Rails.public_path}/")
        html
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ezprint-1.0.1 lib/ezprint/processors/prince.rb
ezprint-1.0.0 lib/ezprint/processors/prince.rb