class PhantomPDF::Generator

Attributes

config[RW]
cookies[R]
exception[R]
input[RW]
options[R]
output[RW]

Public Class Methods

new(input, output=nil, options={}) click to toggle source
# File lib/phantompdf/generator.rb, line 10
def initialize(input, output=nil, options={})
  @input = Source.new(input)
  @output = dump_output(output, !output.nil?)
  @options = Config.new(options).default_options
  @exception = nil
end

Public Instance Methods

generate(path=nil) click to toggle source
# File lib/phantompdf/generator.rb, line 17
def generate(path=nil)
  @output = dump_output(path, true) unless path.nil?

  result = run
  unless $?.exitstatus == 0
    @exception = result.split(/\n/)

    return nil
  end

  result.split("\n").compact.pop
end
generate!(path=nil) click to toggle source
# File lib/phantompdf/generator.rb, line 30
def generate!(path=nil)
  result = generate(path)
  raise RenderingError.new(@exception.join("\n")) if result.nil?

  result
end
to_string() click to toggle source
# File lib/phantompdf/generator.rb, line 37
def to_string
  result = generate(nil)
  return '' if result.nil?

  File.open(result, 'rb').read
end

Protected Instance Methods

dump_args() click to toggle source
# File lib/phantompdf/generator.rb, line 62
def dump_args
  format, header, footer = options[:format], options[:header], options[:footer]
  zoom, margin, orientation = options[:zoom], options[:margin], options[:orientation]
  rendering_timeout, timeout = options[:rendering_timeout], options[:timeout]
  cookies = dump_cookies(options[:cookies])

  [Assets.javascripts('rasterize'),
   @input,
   @output,
   format, dump_header(header), dump_footer(footer),
   margin, orientation, zoom,
   cookies,
   rendering_timeout, timeout].map(&:to_s)
end
dump_cookies(cookies) click to toggle source
# File lib/phantompdf/generator.rb, line 93
def dump_cookies(cookies)
  JSON.dump(cookies)
end
dump_header(header) click to toggle source
# File lib/phantompdf/generator.rb, line 77
def dump_header(header)
  return nil if header.nil? || header.empty?

  return "1.2cm*#{header}" unless header.split('*')[0].to_f > 0

  header
end
dump_output(path, strict) click to toggle source
# File lib/phantompdf/generator.rb, line 49
def dump_output(path, strict)
  if strict
    raise DestinationTypeError.new('Destination must be a valid file path!') unless path.is_a?(String)
    path = File.expand_path(path)
    raise DestinationPermitError.new('Destination does not writable!') unless File.writable?(File.dirname(path))
  else
    path = path.is_a?(String) ? File.expand_path(path) : nil
    path = Tempfile.new('temp_pdf_file').path if path.nil? || !File.writable?(File.dirname(path))
  end

  path
end
run() click to toggle source
# File lib/phantompdf/generator.rb, line 45
def run
  ::Phantomjs.run(*dump_args)
end