Module: PdfGem

Defined in:
lib/pdf_gem.rb,
lib/renderer.rb,
lib/pdf_gem/railtie.rb,
lib/pdf_gem/version.rb

Defined Under Namespace

Classes: PdfGemRailtie, Railtie

Constant Summary collapse

VERSION =
'1.1.1'

Class Method Summary collapse

Class Method Details

.pdf_from_string(params) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/pdf_gem.rb', line 22

def self.pdf_from_string(params)
  tmp = File.join('/', 'tmp')
  Dir.mkdir(tmp) unless File.exist?(tmp)
  html_file = File.join(tmp, "#{rand(36**40).to_s(36)}.html")   
  File.open(html_file, "w+") do |f|        
    f.write(params[:html])
  end
  params[:url] = "file://#{html_file}"
  result = self.pdf_from_url(params)
  File.delete(html_file) if File.exist?(html_file)
  return result
end

.pdf_from_url(params) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/pdf_gem.rb', line 7

def self.pdf_from_url(params)
  stdout, stderr, s = Open3.capture3("node #{File.join(File.dirname(__FILE__), 'pdf_generator.js').to_s}", stdin_data: Base64.strict_encode64(params.to_json).to_s)
  if(s.success?) 
    if(params[:destination].present?)
      FileUtils.mv(stdout, params[:destination])
    else
      res = File.open(stdout, 'rb').read
      File.delete(stdout) if File.exist?(stdout)
      return res
    end
  else
    raise stderr.present? ? stderr : "error"
  end
end