Class: Prince

Inherits:
Object
  • Object
show all
Defined in:
lib/prince-ruby.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePrince

Initialize method



7
8
9
10
11
12
13
# File 'lib/prince-ruby.rb', line 7

def initialize()
  # Finds where the application lives, so we can call it.
  @exe_path = `which prince`.chomp
	@style_sheets = ""
	@log_file = nil
	@options = " --silent --no-network"
end

Instance Attribute Details

#exe_pathObject

Returns fully formed executable path with any command line switches we’ve set based on our variables.



27
28
29
# File 'lib/prince-ruby.rb', line 27

def exe_path
  @exe_path
end

#log_fileObject

Returns the value of attribute log_file.



3
4
5
# File 'lib/prince-ruby.rb', line 3

def log_file
  @log_file
end

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/prince-ruby.rb', line 3

def options
  @options
end

#style_sheetsObject

Returns the value of attribute style_sheets.



3
4
5
# File 'lib/prince-ruby.rb', line 3

def style_sheets
  @style_sheets
end

Instance Method Details

#add_style_sheets(*sheets) ⇒ Object

Sets stylesheets… Can pass in multiple paths for css files.



18
19
20
21
22
# File 'lib/prince-ruby.rb', line 18

def add_style_sheets(*sheets)
  for sheet in sheets do
    @style_sheets << " -s #{sheet} "
  end
end

#html_to_file(string, file) ⇒ Object

Makes a pdf from a passed in string (html) and saves to file specified returns true / false if it everything completed ok



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/prince-ruby.rb', line 52

def html_to_file(string, file)
  path = self.exe_path()
  # set up to take IO as input and output
  path << " --input=html - -o #{file}"
  # Actually call the prince command.
  pdf = IO.popen(path, "w+")
  pdf.puts(string)
  pdf.close
  # see if everything finished ok
  $?.exitstatus == 0
end

#html_to_string(string) ⇒ Object

Makes a pdf from a passed in string (html).



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/prince-ruby.rb', line 36

def html_to_string(string)
  path = self.exe_path()
  # set up to take IO as input and output
  path << " --input=html - -o -"
  # Actually call the prince command, and pass the entire data stream back.
  pdf = IO.popen(path, "w+")
  pdf.puts(string)
  pdf.close_write
  output = pdf.gets(nil)
  pdf.close_read
  return output
end