Module: Html2Pdf

Defined in:
lib/html2pdf/cli.rb,
lib/html2pdf/version.rb,
lib/html2pdf/html2pdf.rb

Defined Under Namespace

Classes: CLI

Constant Summary collapse

VERSION =
"0.1.5"

Class Method Summary collapse

Class Method Details

.softwares_installed?Boolean

Check and return if the ‘wkhtmltopdf’ is available

Returns:

  • (Boolean)


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

def softwares_installed?
  AgileUtils::Helper.which("wkhtmltopdf")
end

.to_pdf(filename) ⇒ Object

Convert ‘*.xhtml’ or ‘*.html’ to pdf

Parameters:

  • filename

    input filename



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/html2pdf/html2pdf.rb', line 20

def to_pdf(filename)
  fail "Invalid input file #{filename}" unless File.exist?(filename)
  # TODO: allow custom configuration
  command = [
    "wkhtmltopdf",
    "--margin-top 4",
    "--margin-bottom 4",
    "--margin-left 4",
    "--margin-right 4",
    '--header-center "[webpage] :: [page]/[topage]"',
    "--header-spacing 1",
    "--header-font-size 8",
    "--header-line",
    "--footer-spacing 1",
    "--footer-font-size 8",
    "--footer-line",
    "#{filename}",
    "#{filename}.pdf",
    "> /dev/null"]
  _stdin, _stderr, status = Open3.capture3(command.join(" "))
  fail "Problem processing #{filename}" unless status.success?
end

.to_pdfs(files) ⇒ Object

Batch convert to pdf using ‘wkhtmltopdf` tool

Parameters:

  • files (Array<String>)

    the input file list

  • base_dir (String)

    the base directory



10
11
12
13
14
15
# File 'lib/html2pdf/html2pdf.rb', line 10

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