Class: PDFKit

Inherits:
Object
  • Object
show all
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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url_file_or_html, options = {}) ⇒ PDFKit

Returns a new instance of PDFKit.

Raises:



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/pdfkit/pdfkit.rb', line 20

def initialize(url_file_or_html, options = {})
  @source = Source.new(url_file_or_html)
  
  @stylesheets = []
  @fix_fonts_for_windows = false  
  
  @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

Class Attribute Details

.configurationObject

Configure PDFKit someplace sensible, like config/initializers/pdfkit.rb

Examples:

PDFKit.configure do |config|
  config.wkhtmltopdf = '/usr/bin/wkhtmltopdf'
end


32
33
34
# File 'lib/pdfkit/configuration.rb', line 32

def configuration
  @configuration
end

Instance Attribute Details

#fix_fonts_for_windowsObject

Returns the value of attribute fix_fonts_for_windows.



17
18
19
# File 'lib/pdfkit/pdfkit.rb', line 17

def fix_fonts_for_windows
  @fix_fonts_for_windows
end

#optionsObject (readonly)

Returns the value of attribute options.



18
19
20
# File 'lib/pdfkit/pdfkit.rb', line 18

def options
  @options
end

#sourceObject

Returns the value of attribute source.



17
18
19
# File 'lib/pdfkit/pdfkit.rb', line 17

def source
  @source
end

#stylesheetsObject

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

Yields:



37
38
39
40
# File 'lib/pdfkit/configuration.rb', line 37

def self.configure
  self.configuration 
  yield(configuration)
end

Instance Method Details

#commandObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/pdfkit/pdfkit.rb', line 33

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

#executableObject



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_pdf(path = nil) ⇒ Object Also known as: to_file



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/pdfkit/pdfkit.rb', line 58

def to_pdf(path=nil)
  append_stylesheets
  fix_all_font_styles_for_windows if @fix_fonts_for_windows == true
  args = command
  args[-1] = path if path && args[-1] == "-"
  invoke = args.map {|arg| %{"#{arg.gsub('"','\\"')}"}} * " "

  result = IO.popen(invoke, "w+") do |pdf|
    pdf.puts(@source.to_s) if @source.html?
    pdf.close_write
    pdf.gets(nil)
  end
  result = File.open(path, "rb") {|file| file.read} if path

  raise "command failed: #{invoke}" if result.to_s.strip.empty?
  return result
end