Class: WeasyPrint

Inherits:
Object
  • Object
show all
Defined in:
lib/weasyprint/source.rb,
lib/weasyprint/version.rb,
lib/weasyprint/middleware.rb,
lib/weasyprint/weasyprint.rb,
lib/weasyprint/configuration.rb

Defined Under Namespace

Classes: Configuration, ImproperSourceError, Middleware, NoExecutableError, Source

Constant Summary collapse

VERSION =
"0.1.0"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of WeasyPrint.

Raises:



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

def initialize(url_file_or_html, options = {})
  @source = Source.new(url_file_or_html)

  @stylesheets = []

  @options = WeasyPrint.configuration.default_options.merge(options)
  @options = normalize_options(@options)

  raise NoExecutableError.new unless File.exists?(WeasyPrint.configuration.weasyprint)
end

Class Attribute Details

.configurationObject

Configure WeasyPrint someplace sensible, like config/initializers/weasyprint.rb

Examples:

WeasyPrint.configure do |config|
  config.weasyprint = '/usr/bin/weasyprint'
end


40
41
42
# File 'lib/weasyprint/configuration.rb', line 40

def configuration
  @configuration
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



20
21
22
# File 'lib/weasyprint/weasyprint.rb', line 20

def options
  @options
end

#sourceObject

Returns the value of attribute source.



19
20
21
# File 'lib/weasyprint/weasyprint.rb', line 19

def source
  @source
end

#stylesheetsObject

Returns the value of attribute stylesheets.



19
20
21
# File 'lib/weasyprint/weasyprint.rb', line 19

def stylesheets
  @stylesheets
end

Class Method Details

.configure {|configuration| ... } ⇒ Object

Yields:



44
45
46
# File 'lib/weasyprint/configuration.rb', line 44

def self.configure
  yield(configuration)
end

Instance Method Details

#command(path = nil) ⇒ Object



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

def command(path = nil)
  args = [executable]
  args += @options.to_a.flatten.compact

  if @source.html?
    args << '-' # Get HTML from stdin
  else
    args << @source.to_s
  end

  args << (path || '-') # Write to file or stdout

  args.shelljoin
end

#executableObject



48
49
50
51
52
53
54
55
56
# File 'lib/weasyprint/weasyprint.rb', line 48

def executable
  default = WeasyPrint.configuration.weasyprint
  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



75
76
77
78
# File 'lib/weasyprint/weasyprint.rb', line 75

def to_file(path)
  self.to_pdf(path)
  File.new(path)
end

#to_pdf(path = nil) ⇒ Object



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

def to_pdf(path=nil)
  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)
  end
  result = File.read(path) if path

  # $? 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 result.to_s.strip.empty? or !successful?($?)
  return result
end