Class: Dhalang::PDF

Inherits:
Object
  • Object
show all
Defined in:
lib/PDF.rb

Constant Summary collapse

PDF_GENERATOR_JS_PATH =
File.expand_path('../js/pdfgenerator.js', __FILE__)
PROJECT_PATH =
Dir.pwd + '/node_modules/'

Class Method Summary collapse

Class Method Details

.get_from_html(html) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/PDF.rb', line 23

def self.get_from_html(html)
  html_file = create_temporary_html_file(html)
  temporary_pdf_save_file = create_temporary_pdf_file
  begin
    visit_page_with_puppeteer("file://" + html_file.path, temporary_pdf_save_file.path)
    binary_pdf_content = get_file_content_as_binary_string(temporary_pdf_save_file)
  ensure
    temporary_pdf_save_file.close unless temporary_pdf_save_file.closed?
    html_file.close unless html_file.closed?
    temporary_pdf_save_file.unlink
    html_file.unlink
  end
  return binary_pdf_content
end

.get_from_url(url) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/PDF.rb', line 10

def self.get_from_url(url)
  validate_url(url)
  temporary_pdf_save_file = create_temporary_pdf_file
  begin
    visit_page_with_puppeteer(url, temporary_pdf_save_file.path)
    binary_pdf_content = get_file_content_as_binary_string(temporary_pdf_save_file)
  ensure
    temporary_pdf_save_file.close unless temporary_pdf_save_file.closed?
    temporary_pdf_save_file.unlink
  end
  return binary_pdf_content
end