Class: RPrince

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRPrince

Returns a new instance of RPrince.



28
29
30
31
# File 'lib/rprince.rb', line 28

def initialize()
  @logger = Logging::Logger[RPrince]
  @style_sheets = []
end

Class Attribute Details

.allow_network_accessObject

Returns the value of attribute allow_network_access.



10
11
12
# File 'lib/rprince.rb', line 10

def allow_network_access
  @allow_network_access
end

.executable_log_pathObject

Returns the value of attribute executable_log_path.



9
10
11
# File 'lib/rprince.rb', line 9

def executable_log_path
  @executable_log_path
end

.executable_pathObject

Returns the value of attribute executable_path.



8
9
10
# File 'lib/rprince.rb', line 8

def executable_path
  @executable_path
end

.verbose_executable_loggingObject

Returns the value of attribute verbose_executable_logging.



11
12
13
# File 'lib/rprince.rb', line 11

def verbose_executable_logging
  @verbose_executable_logging
end

Instance Attribute Details

#style_sheetsObject

Returns the value of attribute style_sheets.



5
6
7
# File 'lib/rprince.rb', line 5

def style_sheets
  @style_sheets
end

Class Method Details

.detect_executable_pathObject



21
22
23
24
25
# File 'lib/rprince.rb', line 21

def detect_executable_path
  IO.popen('which prince') { |pipe| @executable_path = pipe.gets }
  raise "Cannot find prince command-line app in $PATH" unless @executable_path
  @executable_path.strip!
end

Instance Method Details

#html_files_to_pdf_file(files, output_file) ⇒ Object

Since the prince CLI has trouble with long input file paths, we create this method to buffer them through Ruby and to the stdin of our prince process.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rprince.rb', line 35

def html_files_to_pdf_file(files, output_file)
  command = build_command(output_file)
  logger.info("Calling Prince executable with: `#{command}`")
  pdf = IO.popen(command, "w+")
  buffer = ""
  files.each do |path|
    File.open(File.expand_path(path), "r") do |f|
      while ( f.read(1024, buffer) )
        pdf.write(buffer)
      end
    end
  end
  pdf.close
end

#html_string_to_pdf_file(html_string_input, output_file) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/rprince.rb', line 62

def html_string_to_pdf_file(html_string_input, output_file)
  command = build_command(output_file)
  logger.info("Calling Prince executable with: `#{command}`")
  pdf = IO.popen(command, "w+")
  pdf.puts(html_string_input)
  pdf.close
end

#html_string_to_pdf_string(html_string_input) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rprince.rb', line 50

def html_string_to_pdf_string(html_string_input)
  command = build_command
  logger.info("Calling Prince executable with: `#{command}`")
  pdf = IO.popen(command, "w+")
  pdf.puts(html_string_input)
  pdf.close_write
  result = pdf.gets(nil)
  pdf.close_read
  
  result
end