Class: PDFKit
- Inherits:
-
Object
- Object
- PDFKit
- Defined in:
- lib/pdfkit/pdfkit.rb,
lib/pdfkit/source.rb,
lib/pdfkit/version.rb,
lib/pdfkit/middleware.rb,
lib/pdfkit/configuration.rb
Defined Under Namespace
Classes: Configuration, ImproperSourceError, Middleware, NoExecutableError, Source
Constant Summary collapse
- VERSION =
"0.8.0"
Class Attribute Summary collapse
-
.configuration ⇒ Object
Configure PDFKit someplace sensible, like config/initializers/pdfkit.rb.
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#source ⇒ Object
Returns the value of attribute source.
-
#stylesheets ⇒ Object
Returns the value of attribute stylesheets.
Class Method Summary collapse
Instance Method Summary collapse
- #command(path = nil) ⇒ Object
- #executable ⇒ Object
-
#initialize(url_file_or_html, options = {}) ⇒ PDFKit
constructor
A new instance of PDFKit.
- #to_file(path) ⇒ Object
- #to_pdf(path = nil) ⇒ Object
Constructor Details
#initialize(url_file_or_html, options = {}) ⇒ PDFKit
Returns a new instance of PDFKit.
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/pdfkit/pdfkit.rb', line 21 def initialize(url_file_or_html, = {}) @source = Source.new(url_file_or_html) @stylesheets = [] @options = PDFKit.configuration..merge() @options.delete(:quiet) if PDFKit.configuration.verbose? @options.merge! (url_file_or_html) unless source.url? @options = (@options) raise NoExecutableError.new unless File.exists?(PDFKit.configuration.wkhtmltopdf) end |
Class Attribute Details
.configuration ⇒ Object
Configure PDFKit someplace sensible, like config/initializers/pdfkit.rb
47 48 49 |
# File 'lib/pdfkit/configuration.rb', line 47 def configuration @configuration end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
19 20 21 |
# File 'lib/pdfkit/pdfkit.rb', line 19 def @options end |
#source ⇒ Object
Returns the value of attribute source.
18 19 20 |
# File 'lib/pdfkit/pdfkit.rb', line 18 def source @source end |
#stylesheets ⇒ Object
Returns the value of attribute stylesheets.
18 19 20 |
# File 'lib/pdfkit/pdfkit.rb', line 18 def stylesheets @stylesheets end |
Class Method Details
.configure {|configuration| ... } ⇒ Object
51 52 53 |
# File 'lib/pdfkit/configuration.rb', line 51 def self.configure yield(configuration) end |
Instance Method Details
#command(path = nil) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/pdfkit/pdfkit.rb', line 34 def command(path = nil) args = @options.to_a.flatten.compact if @source.html? args << '-' # Get HTML from stdin else args << @source.to_s end args << (path || '-') # Write to file or stdout [executable, args.shelljoin].join ' ' end |
#executable ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/pdfkit/pdfkit.rb', line 48 def executable default = PDFKit.configuration.wkhtmltopdf return default if default !~ /^\// # its not a path, so nothing we can do if File.exist?(default) default else default.split('/').last end end |
#to_file(path) ⇒ Object
75 76 77 78 |
# File 'lib/pdfkit/pdfkit.rb', line 75 def to_file(path) self.to_pdf(path) File.new(path) end |
#to_pdf(path = nil) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/pdfkit/pdfkit.rb', line 58 def to_pdf(path=nil) append_stylesheets invoke = command(path) result = IO.popen(invoke, "wb+") do |pdf| pdf.puts(@source.to_s) if @source.html? pdf.close_write pdf.gets(nil) if path.nil? end # $? is thread safe per # http://stackoverflow.com/questions/2164887/thread-safe-external-process-in-ruby-plus-checking-exitstatus raise "command failed (exitstatus=#{$?.exitstatus}): #{invoke}" if empty_result?(path, result) or !successful?($?) return result end |