Class: Princely::Pdf

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Pdf

Initialize method



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/princely/pdf.rb', line 9

def initialize(options={})
  options = {
    :path => nil,
    :executable => Princely.executable,
    :log_file => nil,
    :logger => nil,
    :server_flag => true,
    :media => nil,
    :javascript_flag => false
  }.merge(options)
  @executable = options[:path] ? Princely::Executable.new(options[:path]) : options[:executable]
  @style_sheets = ''
  @log_file = options[:log_file]
  @logger = options[:logger]
  @server_flag = options[:server_flag]
  @media = options[:media]
  @javascript_flag = options[:javascript_flag]
  @timeout = options[:timeout]
end

Instance Attribute Details

#executableObject

Returns the value of attribute executable.



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

def executable
  @executable
end

#javascript_flagObject

Returns the value of attribute javascript_flag.



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

def javascript_flag
  @javascript_flag
end

#log_fileObject

Returns the instance log file or Princely default log file



35
36
37
# File 'lib/princely/pdf.rb', line 35

def log_file
  @log_file
end

#loggerObject

Returns the instance logger or Princely default logger



30
31
32
# File 'lib/princely/pdf.rb', line 30

def logger
  @logger
end

#mediaObject

Returns the value of attribute media.



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

def media
  @media
end

#server_flagObject

Returns the value of attribute server_flag.



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

def server_flag
  @server_flag
end

#style_sheetsObject

Returns the value of attribute style_sheets.



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

def style_sheets
  @style_sheets
end

#timeoutObject

Returns the value of attribute timeout.



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

def timeout
  @timeout
end

Instance Method Details

#add_style_sheets(*sheets) ⇒ Object

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



42
43
44
# File 'lib/princely/pdf.rb', line 42

def add_style_sheets(*sheets)
  @style_sheets << sheets.map { |sheet| " -s #{sheet} " }.join(' ')
end

#exe_pathObject

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



49
50
51
# File 'lib/princely/pdf.rb', line 49

def exe_path
  @executable.join(executable_options)
end

#executable_optionsObject



53
54
55
56
57
58
59
60
61
62
# File 'lib/princely/pdf.rb', line 53

def executable_options
  options = []
  options << "--input=html"
  options << "--server" if @server_flag
  options << "--log=#{log_file}"
  options << "--media=#{media}" if media
  options << "--javascript" if @javascript_flag
  options << @style_sheets
  options
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.



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/princely/pdf.rb', line 69

def pdf_from_string(string, output_file = '-')
  with_timeout do
    pdf = initialize_pdf_from_string(string, output_file, {:output_to_log_file => false})
    pdf.close_write
    result = pdf.gets(nil)
    pdf.close_read
    result.force_encoding('BINARY') if RUBY_VERSION >= "1.9"

    result
  end
end

#pdf_from_string_to_file(string, output_file) ⇒ Object



81
82
83
84
85
86
# File 'lib/princely/pdf.rb', line 81

def pdf_from_string_to_file(string, output_file)
  with_timeout do
    pdf = initialize_pdf_from_string(string, output_file)
    pdf.close
  end
end