Method: Any2Pdf#pdfkit_html2pdf

Defined in:
lib/any2pdf/html2pdf.rb

#pdfkit_html2pdf(file, outfile, opts) ⇒ Object



7
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/any2pdf/html2pdf.rb', line 7

def pdfkit_html2pdf( file, outfile, opts )
  styles = []
  styles << opts.stylesheet

  style_opts = get_style_options opts.stylesheet

  # find css files
  dom = Nokogiri::parse( File::open(file).read() )
  dom.css("link").each do |e| 
    styles << e.attr("href")
  end

  # update the html links to make sure that local links (relative) work
  ( dom.css("@src") + dom.css("@href") ).
    find_all{|src| (src.value =~ URI::regexp) == nil }.
    each{|src| src.value = File::expand_path(src.value) }

  options = {
    footer_right:   "[page]/[topage]",
    orientation:    (( opts[:landscape] ) ? "landscape" : "portrait" ),
    page_size:      'A4',
  }.merge(style_opts)
  html = dom.to_s
  kit = PDFKit.new(html, options)

  styles.compact.each do |style| 
    kit.stylesheets << style if File::exists?(style)
  end

  kit.to_file(outfile)
end