class WickedPdf

Constants

DEFAULT_BINARY_VERSION
VERSION

Public Class Methods

clear_config() click to toggle source
# File lib/wicked_pdf.rb, line 41
def self.clear_config
  @@config = {}
end
config=(config) click to toggle source
# File lib/wicked_pdf.rb, line 28
def self.config=(config)
  ::Kernel.warn 'WickedPdf.config= is deprecated and will be removed in future versions. Use WickedPdf.configure instead.' unless @@silence_deprecations

  @@config = config
end
configure() { |config| ... } click to toggle source
# File lib/wicked_pdf.rb, line 34
def self.configure
  config = OpenStruct.new(@@config)
  yield config

  @@config.merge! config.to_h
end
new(wkhtmltopdf_binary_path = nil) click to toggle source
# File lib/wicked_pdf.rb, line 45
def initialize(wkhtmltopdf_binary_path = nil)
  @binary = Binary.new(wkhtmltopdf_binary_path, DEFAULT_BINARY_VERSION)
end

Public Instance Methods

binary_version() click to toggle source
# File lib/wicked_pdf.rb, line 49
def binary_version
  @binary.version
end
pdf_from_html_file(filepath, options = {}) click to toggle source
# File lib/wicked_pdf.rb, line 53
def pdf_from_html_file(filepath, options = {})
  pdf_from_url("file:///#{filepath}", options)
end
pdf_from_string(string, options = {}) click to toggle source
# File lib/wicked_pdf.rb, line 57
def pdf_from_string(string, options = {})
  options = options.dup
  options.merge!(WickedPdf.config) { |_key, option, _config| option }
  string_file = WickedPdf::Tempfile.new('wicked_pdf.html', options[:temp_path])
  string_file.write_in_chunks(string)
  pdf_from_html_file(string_file.path, options)
ensure
  if options[:delete_temporary_files] && string_file
    string_file.close!
  elsif string_file
    string_file.close
  end
end
pdf_from_url(url, options = {}) click to toggle source
# File lib/wicked_pdf.rb, line 71
def pdf_from_url(url, options = {}) # rubocop:disable Metrics/CyclomaticComplexity
  # merge in global config options
  options.merge!(WickedPdf.config) { |_key, option, _config| option }
  generated_pdf_file = WickedPdf::Tempfile.new('wicked_pdf_generated_file.pdf', options[:temp_path])
  command = [@binary.path]
  command.unshift(@binary.xvfb_run_path) if options[:use_xvfb]
  command += option_parser.parse(options)
  command << url
  command << generated_pdf_file.path.to_s

  print_command(command.inspect) if in_development_mode?

  if track_progress?(options)
    invoke_with_progress(command, options)
  else
    _out, err, status = Open3.capture3(*command)
    err = [status.to_s, err].join("\n") if !err.empty? || !status.success?
  end
  if options[:return_file]
    return_file = options.delete(:return_file)
    return generated_pdf_file
  end

  pdf = generated_pdf_file.read_in_chunks

  raise "Error generating PDF\n Command Error: #{err}" if options[:raise_on_all_errors] && !err.empty?
  raise "PDF could not be generated!\n Command Error: #{err}" if pdf && pdf.rstrip.empty?

  pdf
rescue StandardError => e
  raise "Failed to execute:\n#{command}\nError: #{e}"
ensure
  clean_temp_files
  generated_pdf_file.close! if generated_pdf_file && !return_file
end

Private Instance Methods

clean_temp_files() click to toggle source
# File lib/wicked_pdf.rb, line 127
def clean_temp_files
  return unless option_parser.hf_tempfiles.present?

  option_parser.hf_tempfiles.each { |file| File.delete(file) }
end
in_development_mode?() click to toggle source
# File lib/wicked_pdf.rb, line 109
def in_development_mode?
  return Rails.env == 'development' if defined?(Rails.env)

  RAILS_ENV == 'development' if defined?(RAILS_ENV)
end
on_windows?() click to toggle source
# File lib/wicked_pdf.rb, line 115
def on_windows?
  RbConfig::CONFIG['target_os'] =~ /mswin|mingw/
end
option_parser() click to toggle source
# File lib/wicked_pdf.rb, line 123
def option_parser
  @option_parser ||= OptionParser.new(binary_version)
end
print_command(cmd) click to toggle source