Class: Pdfgen
- Inherits:
-
Object
- Object
- Pdfgen
- Defined in:
- lib/pdfgen.rb
Instance Method Summary collapse
- #debug_mode(debug_time) ⇒ Object
- #emulate_media(media_type) ⇒ Object
-
#initialize(html_or_url) ⇒ Pdfgen
constructor
A new instance of Pdfgen.
- #launch_options(launch_options) ⇒ Object
- #set_viewport(viewport_options) ⇒ Object
- #to_pdf(opts = {}) ⇒ Object
- #url_options(url_options) ⇒ Object
- #wait_for_timeout(wait_for_timeout) ⇒ Object
Constructor Details
#initialize(html_or_url) ⇒ Pdfgen
Returns a new instance of Pdfgen.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/pdfgen.rb', line 15 def initialize(html_or_url) if html_or_url =~ /\Ahttp|\Afile/ @url = html_or_url @html = nil else @url = nil @html = html_or_url end = nil @emulate_media = nil = Hash.new @wait_for_timeout = nil @debug_time = nil = { waitUntil: 'networkidle0' } end |
Instance Method Details
#debug_mode(debug_time) ⇒ Object
52 53 54 55 56 |
# File 'lib/pdfgen.rb', line 52 def debug_mode(debug_time) raise TypeError.new("Timeout must be an integer or respond to #to_i") unless debug_time.kind_of?(Integer) || (debug_time.respond_to?(:to_i) && debug_time.to_i) @debug_time = debug_time self end |
#emulate_media(media_type) ⇒ Object
36 37 38 39 |
# File 'lib/pdfgen.rb', line 36 def emulate_media(media_type) @emulate_media = media_type self end |
#launch_options(launch_options) ⇒ Object
41 42 43 44 |
# File 'lib/pdfgen.rb', line 41 def () = self end |
#set_viewport(viewport_options) ⇒ Object
31 32 33 34 |
# File 'lib/pdfgen.rb', line 31 def () = self end |
#to_pdf(opts = {}) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/pdfgen.rb', line 63 def to_pdf(opts = {}) = { pdf_options: opts, current_path: Dir.pwd } = .merge(viewport_options: ) if = .merge(emulate_media: @emulate_media) if @emulate_media = .merge(wait_for_timeout: @wait_for_timeout) if @wait_for_timeout if @debug_time = .merge(wait_for_timeout: @debug_time) = .merge(launch_options: .merge(headless: false)) = .merge(debug_mode: true) else = .merge(launch_options: ) end pdf_output = nil error_output = nil status = nil if @html file = Tempfile.new('input_html') file.write(@html) file.close pdf_output, error_output, status = Open3.capture3(MAKE_PDF_COMMAND, file.path, stdin_data: .to_json) file.unlink else = .merge(url: @url) = .merge(url_options: ) pdf_output, error_output, status = Open3.capture3(MAKE_PDF_COMMAND, stdin_data: .to_json) end unless status.success? raise "This error was encountered running node to create the pdf: #{error_output}" end unless pdf_output raise 'There was an error creating the temporary file used to pass the HTML to node.' end pdf_output end |
#url_options(url_options) ⇒ Object
58 59 60 61 |
# File 'lib/pdfgen.rb', line 58 def () = .merge() self end |
#wait_for_timeout(wait_for_timeout) ⇒ Object
46 47 48 49 50 |
# File 'lib/pdfgen.rb', line 46 def wait_for_timeout(wait_for_timeout) raise TypeError.new("Timeout must be an integer or respond to #to_i") unless wait_for_timeout.kind_of?(Integer) || (wait_for_timeout.respond_to?(:to_i) && wait_for_timeout.to_i) @wait_for_timeout = wait_for_timeout self end |