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, options) ⇒ 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, options)
  params = [
    '--page-size A4',
    '--margin-bottom 10mm',
    '--margin-top 10mm',
    '--margin-left 20mm',
    '--margin-right 20mm',
    '--footer-left "[section]"',
    '--footer-right "[page] / [toPage]"',
    '--footer-font-size 7',
    '--footer-spacing 4'
  ]

  unless options[:cover_file] === ''
    params << "cover \"#{cover_html_file.file_name}\""
  end

  if options[:toc]
    xsl = File.read(File.expand_path(File.dirname(__FILE__)) + '/../../toc.xsl')
    xsl.gsub! '[[toc_headline]]', options[: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

  return output
end