module Html2Pdf

Constants

VERSION

Attributes

configuration[RW]

Public Class Methods

configure() { |configuration| ... } click to toggle source
# File lib/html2pdf/configuration.rb, line 45
def configure
  yield(configuration)
end
softwares_installed?() click to toggle source

Check and return if the 'wkhtmltopdf' is available

# File lib/html2pdf/html2pdf.rb, line 33
def softwares_installed?
  AgileUtils::Helper.which("wkhtmltopdf")
end
to_pdf(filename) click to toggle source

Convert '*.xhtml' or '*.html' to pdf

@param filename input filename

# File lib/html2pdf/html2pdf.rb, line 17
def to_pdf(filename)
  fail "Invalid input file #{filename}" unless File.exist?(filename)
  wkhtmltopdf   = Html2Pdf.configuration.options[:wkhtmltopdf]
  page_settings = Html2Pdf.configuration.options[:page_settings]
  command = [
    wkhtmltopdf,
    *page_settings,
    filename,
    "#{filename}.pdf",
    "> /dev/null"
  ]
  _stdin, _stderr, status = Open3.capture3(command.join(" "))
  fail "Problem processing #{filename}" unless status.success?
end
to_pdfs(files) click to toggle source

Batch convert to pdf using `wkhtmltopdf` tool

@param [Array<String>] files the input file list @param [String] base_dir the base directory

# File lib/html2pdf/html2pdf.rb, line 7
def to_pdfs(files)
  files.each_with_index do |file, index|
    puts "Convert file #{index + 1} of #{files.size} : #{file}"
    to_pdf(file)
  end
end
update_config() click to toggle source

Customize the configuration for specific system (Ubuntu/OSX/etc) See: ./lib/html2pdf/configuration.rb for available options rubocop:disable MethodLength

# File lib/html2pdf/config/html2pdf.rb, line 6
def update_config
  Html2Pdf.configure do |config|
    # Note: or add your own path here if `wkhtmltopdf` is not in the $PATH environment
    config.options[:wkhtmltopdf]   = (defined?(Bundler::GemfileError) ? `bundle exec which wkhtmltopdf` : `which wkhtmltopdf`).chomp
    config.options[:page_settings] = [
      "--margin-top 8",
      "--margin-bottom 8",
      "--margin-left 8",
      "--margin-right 8",
      '--header-center "[webpage] :: [page]/[topage]"',
      "--header-spacing 1",
      "--header-font-size 8",
      "--header-line",
      "--footer-spacing 1",
      "--footer-font-size 8",
      "--footer-line"
    ]
  end
end