Module: H2P

Defined in:
lib/h2p.rb,
lib/h2p/version.rb

Constant Summary collapse

VERSION =
'0.2'

Class Method Summary collapse

Class Method Details

.convert(html) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/h2p.rb', line 16

def self.convert(html)
  html_path = tmp_path(:html)
  pdf_path = tmp_path(:pdf)
  File.open(html_path, 'w+') { |f| f << html }
  system('wkhtmltopdf', '-q', html_path, pdf_path)
  IO.read(pdf_path)
ensure
  FileUtils.rm(html_path) rescue nil
  FileUtils.rm(pdf_path) rescue nil
end

.tmp_path(ext) ⇒ Object



10
11
12
13
14
# File 'lib/h2p.rb', line 10

def self.tmp_path(ext)
  stamp = Time.now.to_f
  @counter ||= 0
  File.join(Dir.tmpdir, "h2p-#{stamp}-#{@counter += 1}.#{ext}")
end