Class: Riven::Wkhtmltopdf

Inherits:
Object
  • Object
show all
Defined in:
lib/riven/wkhtmltopdf.rb

Instance Method Summary collapse

Instance Method Details

#check_installationObject



4
5
6
7
8
9
10
11
# File 'lib/riven/wkhtmltopdf.rb', line 4

public def check_installation
  `wkhtmltopdf -V > /dev/null 2>&1`

  unless $?.exitstatus == 0
    puts 'Seems like wkhtmltopdf is not correctly installed or set up'
    exit
  end
end

#generate_pdf(html_file, cover_html_file, output_file, config) ⇒ Object



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
38
39
40
41
42
43
44
45
# File 'lib/riven/wkhtmltopdf.rb', line 13

public def generate_pdf(html_file, cover_html_file, output_file, config)
  params = [
    '--page-size A4',
    '--margin-bottom 20mm',
    '--margin-top 20mm',
    '--margin-left 30mm',
    '--margin-right 30mm',
    '--footer-left "[section]"',
    '--footer-right "[page] / [toPage]"',
    '--footer-font-size 7',
    '--footer-spacing 10'
  ]

  unless config.cover_file === ''
    params << "cover \"#{cover_html_file.file_name}\""
  end

  if config.generate_toc
    xsl = File.read(File.expand_path(File.dirname(__FILE__)) + '/../../toc.xsl')
    xsl.gsub! '[[toc_headline]]', config.toc_headline
    xsl_file_name = '_tmp_toc.xsl'
    File.open(xsl_file_name, 'w') { |file| file.write(xsl) }

    params << 'toc'
    params << "--xsl-style-sheet \"#{xsl_file_name}\""
  end

  output = `wkhtmltopdf #{params.join(' ')} "#{html_file.file_name}" "#{output_file}" 2>&1`

  File.delete xsl_file_name if config.generate_toc

  output
end