lib/pdfkit/pdfkit.rb in pdfkit-0.8.4.1 vs lib/pdfkit/pdfkit.rb in pdfkit-0.8.4.2
- old
+ new
@@ -1,22 +1,30 @@
require 'shellwords'
class PDFKit
- class NoExecutableError < StandardError
+ class Error < StandardError; end
+
+ class NoExecutableError < Error
def initialize
msg = "No wkhtmltopdf executable found at #{PDFKit.configuration.wkhtmltopdf}\n"
msg << ">> Please install wkhtmltopdf - https://github.com/pdfkit/PDFKit/wiki/Installing-WKHTMLTOPDF"
super(msg)
end
end
- class ImproperSourceError < StandardError
+ class ImproperSourceError < Error
def initialize(msg)
super("Improper Source: #{msg}")
end
end
+ class ImproperWkhtmltopdfExitStatus < Error
+ def initialize(invoke)
+ super("Command failed (exitstatus=#{$?.exitstatus}): #{invoke}")
+ end
+ end
+
attr_accessor :source, :stylesheets
attr_reader :renderer
def initialize(url_file_or_html, options = {})
@source = Source.new(url_file_or_html)
@@ -29,11 +37,11 @@
@root_url = options.delete(:root_url)
@protocol = options.delete(:protocol)
@renderer = WkHTMLtoPDF.new options
@renderer.normalize_options
- raise NoExecutableError.new unless File.exists?(PDFKit.configuration.wkhtmltopdf)
+ raise NoExecutableError unless File.exists?(PDFKit.configuration.wkhtmltopdf)
end
def command(path = nil)
args = @renderer.options_for_command
shell_escaped_command = [executable, OS::shell_escape_for_os(args)].join ' '
@@ -68,11 +76,11 @@
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?($?)
+ raise ImproperWkhtmltopdfExitStatus, invoke if empty_result?(path, result) || !successful?($?)
return result
end
def to_file(path)
self.to_pdf(path)
@@ -81,11 +89,11 @@
protected
def find_options_in_meta(content)
# Read file if content is a File
- content = content.read if content.is_a?(File)
+ content = content.read if content.is_a?(File) || content.is_a?(Tempfile)
found = {}
content.scan(/<meta [^>]*>/) do |meta|
if meta.match(/name=["']#{PDFKit.configuration.meta_tag_prefix}/)
name = meta.scan(/name=["']#{PDFKit.configuration.meta_tag_prefix}([^"']*)/)[0][0].split
@@ -114,10 +122,10 @@
@source = Source.new(processed_html)
end
end
def append_stylesheets
- raise ImproperSourceError.new('Stylesheets may only be added to an HTML source') if stylesheets.any? && !@source.html?
+ raise ImproperSourceError, '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 = Source.new(@source.to_s.gsub(/(<\/head>)/) {|s| style_tag_for(stylesheet) + s })
else