Module: Html2Pdf

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

Defined Under Namespace

Classes: CLI, Configuration

Constant Summary collapse

VERSION =
"0.2.1"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject

Configure Pdfs2Pdf someplace sensible, like config/initializers/html2pdf.rb

Html2Pdf.configure do |config|

# set appropriate options
config.options[:wkhtmltopdf]   = '/usr/bin/wkhtmltopdf'
config.options[:page_settings] = [ "--margin-top 4",
                                   "--margin-bottom 4",
                                   ..
                                 ]

end



41
42
43
# File 'lib/html2pdf/configuration.rb', line 41

def configuration
  @configuration
end

Class Method Details

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

Yields:



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

def configure
  yield(configuration)
end

.softwares_installed?Boolean

Check and return if the ‘wkhtmltopdf’ is available

Returns:

  • (Boolean)


37
38
39
# File 'lib/html2pdf/html2pdf.rb', line 37

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

.to_pdf(filename) ⇒ Object

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

Parameters:

  • filename

    input filename



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/html2pdf/html2pdf.rb', line 21

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) ⇒ Object

Batch convert to pdf using ‘wkhtmltopdf` tool

Parameters:

  • files (Array<String>)

    the input file list

  • base_dir (String)

    the base directory



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

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