Class: Princely

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

Constant Summary collapse

VERSION =
"1.0.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePrincely

Initialize method



27
28
29
30
31
32
33
34
# File 'lib/fa_princely.rb', line 27

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 = "#{RAILS_ROOT}/log/prince.log"
	@logger = RAILS_DEFAULT_LOGGER
end

Instance Attribute Details

#exe_pathObject

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



48
49
50
# File 'lib/fa_princely.rb', line 48

def exe_path
  @exe_path
end

#log_fileObject

Returns the value of attribute log_file.



23
24
25
# File 'lib/fa_princely.rb', line 23

def log_file
  @log_file
end

#loggerObject

Returns the value of attribute logger.



23
24
25
# File 'lib/fa_princely.rb', line 23

def logger
  @logger
end

#style_sheetsObject

Returns the value of attribute style_sheets.



23
24
25
# File 'lib/fa_princely.rb', line 23

def style_sheets
  @style_sheets
end

Instance Method Details

#add_style_sheets(*sheets) ⇒ Object

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



39
40
41
42
43
# File 'lib/fa_princely.rb', line 39

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.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/fa_princely.rb', line 61

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 -'
  
  # Show the command used...
  logger.info "\n\nPRINCE XML PDF COMMAND"
  logger.info path
  logger.info ''
  
  # 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



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/fa_princely.rb', line 81

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}"
  
  # Show the command used...
  logger.info "\n\nPRINCE XML PDF COMMAND"
  logger.info path
  logger.info ''
  
  # Actually call the prince command, and pass the entire data stream back.
  pdf = IO.popen(path, "w+")
  pdf.puts(string)
  pdf.close
end