Class: Html2Pdf::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/html2pdf/cli.rb

Instance Method Summary collapse

Instance Method Details

#exportObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/html2pdf/cli.rb', line 8

def export
  opts = options.deep_symbolize_keys

  unless Html2Pdf.softwares_installed?
    fail "You must have valid `wkhtmltopdf` installation"
  end

  if opts[:version]
    puts "You are using Html2Pdf version #{Html2Pdf::VERSION}"
    exit
  end

  # Always expand path so that the '.' or '~' will work if specified
  base_dir = File.expand_path(opts[:base_dir])
  input_files = CodeLister.files base_dir: base_dir,
                                 exts: %w[html xhtml],
                                 recursive: opts[:recursive]
  elapsed = AgileUtils::FileUtil.time do
    # Change the directory to the base diretory as the input_files is always
    # start with '.' relative to the base_dir
    FileUtils.cd(base_dir)
    Html2Pdf.to_pdfs(input_files)
  end
  generated_files = add_suffix(input_files, "pdf")
  output_file = "html2pdf_#{File.basename(File.expand_path(base_dir))}.tar.gz"
  AgileUtils::FileUtil.tar_gzip_files(generated_files, output_file)
  AgileUtils::FileUtil.delete(generated_files)
  puts "Convert files to pdfs took #{elapsed} ms"
  puts "Your final output is '#{File.absolute_path(output_file)}'"
end

#usageObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/html2pdf/cli.rb', line 41

def usage
  puts "Usage:\nhtml2pdf\n\nOptions:\n-b, [--base-dir=BASE_DIR]                # Base directory\n# Default: . (current directory)\n-r, [--recursive], [--no-recursive]      # Search for files recursively\n# Default: --recursive\n-v, [--version], [--no-version]          # Display version information\n\nexport multiple html files to pdfs\n"
end