Sha256: 7f425e438e18d8292819f1e5dbfa22aef9deaa06aa40ab2774b9d117de72186e

Contents?: true

Size: 575 Bytes

Versions: 1

Compression:

Stored size: 575 Bytes

Contents

# frozen_string_literal: true

require 'modulation/gem'
require 'tmpdir'
require 'fileutils'

export_default :H2P

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

  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
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
h2p-0.2 lib/h2p.rb