Class: PrinceXML

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

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.asset_domain=(domain) ⇒ Object (writeonly)

Sets the attribute asset_domain

Parameters:

  • value

    the value to set the attribute asset_domain to.



3
4
5
# File 'lib/to_pdf/prince_xml.rb', line 3

def asset_domain=(value)
  @asset_domain = value
end

.executable_path=(path) ⇒ Object (writeonly)

Sets the attribute executable_path

Parameters:

  • value

    the value to set the attribute executable_path to.



3
4
5
# File 'lib/to_pdf/prince_xml.rb', line 3

def executable_path=(value)
  @executable_path = value
end

Class Method Details

.detect_executable_pathObject



13
14
15
16
17
# File 'lib/to_pdf/prince_xml.rb', line 13

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

.localise_paths(string) ⇒ Object



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

def localise_paths(string)
  string.gsub!('src="/', "src=\"#{Rails.public_path}/")

  if @asset_domain
    string.gsub!('link href="/', "link href=\"#{@asset_domain}/")
  elsif defined?(Rails)
    string.gsub!('link href="/', "link href=\"#{Rails.public_path}/")
    string.gsub!('url(/', "url(#{Rails.public_path}/")
  end

  string
end

.prince_commandObject



31
32
33
# File 'lib/to_pdf/prince_xml.rb', line 31

def prince_command
  "#{@executable_path} --input=html --silent - -o '-'"
end

.string_to_pdf(string) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/to_pdf/prince_xml.rb', line 19

def string_to_pdf(string)
  detect_executable_path unless @executable_path

  pdf = IO.popen(prince_command, 'w+')
  pdf.puts(localise_paths(string))
  pdf.close_write
  result = pdf.gets(nil)
  pdf.close_read

  result
end