Class: Princely

Inherits:
Object
  • Object
show all
Defined in:
lib/princely.rb

Constant Summary collapse

VERSION =
"1.0.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePrincely

Initialize method



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

def initialize()
  # Finds where the application lives, so we can call it.
  @exe_path = `which prince`.chomp
  raise "Cannot find prince command-line app in $PATH" if @exe_path.length == 0
	@style_sheets = ''
	@log_file = "/tmp/prince.log"
end

Instance Attribute Details

#exe_pathObject

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



46
47
48
# File 'lib/princely.rb', line 46

def exe_path
  @exe_path
end

#log_fileObject

Returns the value of attribute log_file.



22
23
24
# File 'lib/princely.rb', line 22

def log_file
  @log_file
end

#style_sheetsObject

Returns the value of attribute style_sheets.



22
23
24
# File 'lib/princely.rb', line 22

def style_sheets
  @style_sheets
end

Instance Method Details

#add_style_sheets(*sheets) ⇒ Object

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



37
38
39
40
41
# File 'lib/princely.rb', line 37

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

#pdf_from_string(string, output_file = '-') ⇒ Object

Makes a pdf from a passed in string.

Returns PDF as a stream, so we can use send_data to shoot it down the pipe using Rails.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/princely.rb', line 58

def pdf_from_string(string, output_file = '-')
  path = self.exe_path()
  # Don't spew errors to the standard out...and set up to take IO 
  # as input and output
  path << ' --silent - -o -'

  # Actually call the prince command, and pass the entire data stream back.
  pdf = IO.popen(path, "w+")
  pdf.puts(string)
  pdf.close_write
  result = pdf.gets(nil)
  pdf.close_read
  return result
end

#pdf_from_string_to_file(string, output_file) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/princely.rb', line 73

def pdf_from_string_to_file(string, output_file)
  path = self.exe_path()
  # Don't spew errors to the standard out...and set up to take IO 
  # as input and output
  path << " --silent - -o '#{output_file}' >> '#{@log_file}' 2>> '#{@log_file}'"

  # Actually call the prince command, and pass the entire data stream back.
  pdf = IO.popen(path, "w+")
  pdf.puts(string)
  pdf.close
end