Object
# File lib/pdfkit/configuration.rb, line 37 def self.configure self.configuration yield(configuration) end
# File lib/pdfkit/pdfkit.rb, line 20 def initialize(url_file_or_html, options = {}) @source = Source.new(url_file_or_html) @stylesheets = [] @options = PDFKit.configuration.default_options.merge(options) @options.merge! find_options_in_meta(url_file_or_html) unless source.url? @options = normalize_options(@options) raise NoExecutableError.new unless File.exists?(PDFKit.configuration.wkhtmltopdf) end
# 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
# 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
# File lib/pdfkit/pdfkit.rb, line 71 def to_file(path) File.open(path,'w') {|file| file << self.to_pdf} end
# 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.join(' ')}" if result.to_s.strip.empty? return result end
# File lib/pdfkit/pdfkit.rb, line 100 def append_stylesheets raise ImproperSourceError.new('Stylesheets may only be added to an HTML source') if stylesheets.any? && !@source.html? stylesheets.each do |stylesheet| if @source.to_s.match(/<\/head>/) @source.to_s.gsub!(/(<\/head>)/, style_tag_for(stylesheet)+'\1') else @source.to_s.insert(0, style_tag_for(stylesheet)) end end end
# File lib/pdfkit/pdfkit.rb, line 77 def find_options_in_meta(body) pdfkit_meta_tags(body).inject({}) do |found, tag| name = tag.attributes["name"].sub(/^#{PDFKit.configuration.meta_tag_prefix}/, '').to_sym found.merge(name => tag.attributes["content"]) end end
# File lib/pdfkit/pdfkit.rb, line 123 def normalize_arg(arg) arg.to_s.downcase.gsub(/[^a-z0-9]/,'-') end
# File lib/pdfkit/pdfkit.rb, line 112 def normalize_options(options) normalized_options = {} options.each do |key, value| next if !value normalized_key = "--#{normalize_arg key}" normalized_options[normalized_key] = normalize_value(value) end normalized_options end
Generated with the Darkfish Rdoc Generator 2.