Class: Ezprint::Processors::Prince

Inherits:
Base
  • Object
show all
Defined in:
lib/ezprint/processors/prince.rb

Class Method Summary collapse

Class Method Details

.cmd(options) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ezprint/processors/prince.rb', line 13

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

.process(html_string, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/ezprint/processors/prince.rb', line 4

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

.process_html(html) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/ezprint/processors/prince.rb', line 26

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