Class: Princely::Pdf
- Inherits:
-
Object
- Object
- Princely::Pdf
- Defined in:
- lib/princely/pdf.rb
Instance Attribute Summary collapse
-
#executable ⇒ Object
Returns the value of attribute executable.
-
#javascript_flag ⇒ Object
Returns the value of attribute javascript_flag.
-
#log_file ⇒ Object
Returns the instance log file or Princely default log file.
-
#logger ⇒ Object
Returns the instance logger or Princely default logger.
-
#media ⇒ Object
Returns the value of attribute media.
-
#server_flag ⇒ Object
Returns the value of attribute server_flag.
-
#style_sheets ⇒ Object
Returns the value of attribute style_sheets.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Instance Method Summary collapse
-
#add_style_sheets(*sheets) ⇒ Object
Sets stylesheets…
-
#exe_path ⇒ Object
Returns fully formed executable path with any command line switches we’ve set based on our variables.
- #executable_options ⇒ Object
-
#initialize(options = {}) ⇒ Pdf
constructor
Initialize method.
-
#pdf_from_string(string, output_file = '-') ⇒ Object
Makes a pdf from a passed in string.
- #pdf_from_string_to_file(string, output_file) ⇒ Object
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(={}) = { :path => nil, :executable => Princely.executable, :log_file => nil, :logger => nil, :server_flag => true, :media => nil, :javascript_flag => false }.merge() @executable = [:path] ? Princely::Executable.new([:path]) : [:executable] @style_sheets = '' @log_file = [:log_file] @logger = [:logger] @server_flag = [:server_flag] @media = [:media] @javascript_flag = [:javascript_flag] @timeout = [:timeout] end |
Instance Attribute Details
#executable ⇒ Object
Returns the value of attribute executable.
5 6 7 |
# File 'lib/princely/pdf.rb', line 5 def executable @executable end |
#javascript_flag ⇒ Object
Returns the value of attribute javascript_flag.
5 6 7 |
# File 'lib/princely/pdf.rb', line 5 def javascript_flag @javascript_flag end |
#log_file ⇒ Object
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 |
#logger ⇒ Object
Returns the instance logger or Princely default logger
30 31 32 |
# File 'lib/princely/pdf.rb', line 30 def logger @logger end |
#media ⇒ Object
Returns the value of attribute media.
5 6 7 |
# File 'lib/princely/pdf.rb', line 5 def media @media end |
#server_flag ⇒ Object
Returns the value of attribute server_flag.
5 6 7 |
# File 'lib/princely/pdf.rb', line 5 def server_flag @server_flag end |
#style_sheets ⇒ Object
Returns the value of attribute style_sheets.
5 6 7 |
# File 'lib/princely/pdf.rb', line 5 def style_sheets @style_sheets end |
#timeout ⇒ Object
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_path ⇒ Object
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() end |
#executable_options ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/princely/pdf.rb', line 53 def = [] << "--input=html" << "--server" if @server_flag << "--log=#{log_file}" << "--media=#{media}" if media << "--javascript" if @javascript_flag << @style_sheets 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 |