Module: Html2Pdf

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

Defined Under Namespace

Classes: CLI, Configuration

Constant Summary collapse

VERSION =
"0.2.6"

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)


33
34
35
# File 'lib/html2pdf/html2pdf.rb', line 33

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

.to_pdf(filename) ⇒ Object

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

Parameters:

  • filename

    input filename



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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) ⇒ Object

Batch convert to pdf using ‘wkhtmltopdf` tool

Parameters:

  • files (Array<String>)

    the input file list

  • base_dir (String)

    the base directory



7
8
9
10
11
12
# 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_configObject

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



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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