Class: PDFKit
- Inherits:
-
Object
- Object
- PDFKit
- Defined in:
- lib/pdfkit/pdfkit.rb,
lib/pdfkit/source.rb,
lib/pdfkit/middleware.rb,
lib/pdfkit/configuration.rb
Defined Under Namespace
Classes: Configuration, ImproperSourceError, Middleware, NoExecutableError, Source
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 ⇒ Object
- #executable ⇒ Object
-
#initialize(url_file_or_html, options = {}) ⇒ PDFKit
constructor
A new instance of PDFKit.
- #to_file(path) ⇒ Object
- #to_pdf ⇒ Object
Constructor Details
#initialize(url_file_or_html, options = {}) ⇒ PDFKit
Returns a new instance of PDFKit.
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/pdfkit/pdfkit.rb', line 20 def initialize(url_file_or_html, = {}) @source = Source.new(url_file_or_html) @stylesheets = [] @options = PDFKit.configuration..merge() @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
31 32 33 |
# File 'lib/pdfkit/configuration.rb', line 31 def configuration @configuration end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
18 19 20 |
# File 'lib/pdfkit/pdfkit.rb', line 18 def @options end |
#source ⇒ Object
Returns the value of attribute source.
17 18 19 |
# File 'lib/pdfkit/pdfkit.rb', line 17 def source @source end |
#stylesheets ⇒ Object
Returns the value of attribute stylesheets.
17 18 19 |
# File 'lib/pdfkit/pdfkit.rb', line 17 def stylesheets @stylesheets end |
Class Method Details
.configure {|configuration| ... } ⇒ Object
36 37 38 39 |
# File 'lib/pdfkit/configuration.rb', line 36 def self.configure self.configuration yield(configuration) end |
Instance Method Details
#command ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/pdfkit/pdfkit.rb', line 32 def command args = [executable] args += @options.to_a.flatten.compact args << '--quiet' if @source.html? args << '-' # Get HTML from stdin else args << @source.to_s end args << '-' # Read PDF from stdout args end |
#executable ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/pdfkit/pdfkit.rb', line 47 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
71 72 73 |
# File 'lib/pdfkit/pdfkit.rb', line 71 def to_file(path) File.open(path,'w') {|file| file << self.to_pdf} end |
#to_pdf ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/pdfkit/pdfkit.rb', line 57 def to_pdf append_stylesheets pdf = Kernel.open('|-', "w+") exec(*command) if pdf.nil? pdf.puts(@source.to_s) if @source.html? pdf.close_write result = pdf.gets(nil) pdf.close_read raise "command failed: #{command}" if result.to_s.strip.empty? return result end |