Class: MyPDFKit
- Inherits:
-
Object
- Object
- MyPDFKit
- Defined in:
- lib/my_pdfkit/os.rb,
lib/my_pdfkit/pdfkit.rb,
lib/my_pdfkit/source.rb,
lib/my_pdfkit/version.rb,
lib/my_pdfkit/middleware.rb,
lib/my_pdfkit/wkhtmltopdf.rb,
lib/my_pdfkit/configuration.rb,
lib/my_pdfkit/html_preprocessor.rb
Defined Under Namespace
Modules: HTMLPreprocessor, OS Classes: Configuration, Error, ImproperSourceError, ImproperWkhtmltopdfExitStatus, Middleware, NoExecutableError, Source, WkHTMLtoPDF
Constant Summary collapse
- VERSION =
'0.1.0.0'
Class Attribute Summary collapse
-
.configuration ⇒ Object
Configure MyPDFKit someplace sensible, like config/initializers/my_pdfkit.rb.
Instance Attribute Summary collapse
-
#renderer ⇒ Object
readonly
Returns the value of attribute renderer.
-
#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 = {}) ⇒ MyPDFKit
constructor
A new instance of MyPDFKit.
- #options ⇒ Object
- #to_file(path) ⇒ Object
- #to_pdf(path = nil) ⇒ Object
Constructor Details
#initialize(url_file_or_html, options = {}) ⇒ MyPDFKit
Returns a new instance of MyPDFKit.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/my_pdfkit/pdfkit.rb', line 32 def initialize(url_file_or_html, = {}) @source = Source.new(url_file_or_html) @stylesheets = [] = MyPDFKit.configuration..merge() .delete(:quiet) if MyPDFKit.configuration.verbose? .merge! (url_file_or_html) unless source.url? @root_url = .delete(:root_url) @protocol = .delete(:protocol) @renderer = WkHTMLtoPDF.new @renderer. raise NoExecutableError unless File.exist?(MyPDFKit.configuration.wkhtmltopdf) end |
Class Attribute Details
.configuration ⇒ Object
Configure MyPDFKit someplace sensible, like config/initializers/my_pdfkit.rb
82 83 84 |
# File 'lib/my_pdfkit/configuration.rb', line 82 def configuration @configuration end |
Instance Attribute Details
#renderer ⇒ Object (readonly)
Returns the value of attribute renderer.
30 31 32 |
# File 'lib/my_pdfkit/pdfkit.rb', line 30 def renderer @renderer end |
#source ⇒ Object
Returns the value of attribute source.
29 30 31 |
# File 'lib/my_pdfkit/pdfkit.rb', line 29 def source @source end |
#stylesheets ⇒ Object
Returns the value of attribute stylesheets.
29 30 31 |
# File 'lib/my_pdfkit/pdfkit.rb', line 29 def stylesheets @stylesheets end |
Class Method Details
.configure {|configuration| ... } ⇒ Object
86 87 88 |
# File 'lib/my_pdfkit/configuration.rb', line 86 def self.configure yield(configuration) end |
Instance Method Details
#command(path = nil) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/my_pdfkit/pdfkit.rb', line 48 def command(path = nil) args = [*executable] args.concat(@renderer.) args << @source.to_input_for_command args << (path ? path.to_s : '-') args end |
#executable ⇒ Object
61 62 63 |
# File 'lib/my_pdfkit/pdfkit.rb', line 61 def executable MyPDFKit.configuration.executable end |
#options ⇒ Object
56 57 58 59 |
# File 'lib/my_pdfkit/pdfkit.rb', line 56 def # TODO(cdwort,sigmavirus24): Replace this with an attr_reader for @renderer instead in 1.0.0 @renderer. end |
#to_file(path) ⇒ Object
83 84 85 86 |
# File 'lib/my_pdfkit/pdfkit.rb', line 83 def to_file(path) self.to_pdf(path) File.new(path) end |
#to_pdf(path = nil) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/my_pdfkit/pdfkit.rb', line 65 def to_pdf(path=nil) preprocess_html 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 ImproperWkhtmltopdfExitStatus, invoke if empty_result?(path, result) || !successful?($?) return result end |