Sha256: a68d07c1dd98cde7135fd108324504003797beb9f20bd17d35bd8cb8f9b71fe0
Contents?: true
Size: 1.47 KB
Versions: 3
Compression:
Stored size: 1.47 KB
Contents
class HtmlTo::HtmlHeadless require 'fileutils' require 'html_to/chromium_controll.rb' include HtmlTo::ChromiumControll def to_image(obj, width=1200, height=630) width = obj.class.class_variable_get(:@@html_to_width) if obj.class.class_variable_defined?(:@@html_to_width) height = obj.class.class_variable_get(:@@html_to_height) if obj.class.class_variable_defined?(:@@html_to_height) @template = File.read(Rails.root.join('app/views').join(obj.class.class_variable_get(:@@share_template)+".html.erb")) html = ERB.new(@template.html_safe).result(binding) File.open(html_file_path, 'w+') {|f| f.write(html) } begin cmd = "'#{chrome}' --headless --screenshot=#{screenshot_file_path} --window-size=#{width},#{height} --disable-gpu --disable-features=NetworkService #{html_file_path}".gsub("\n",' ') `#{cmd}` if $?.success? obj.skip_share_image_generate = true obj.send("#{obj.class.class_variable_get(:@@share_uploader)}=", File.open(screenshot_file_path)) obj.save else raise "result = #{$?}; command = #{cmd}" end ensure FileUtils.rm(html_file_path) FileUtils.rm(screenshot_file_path) end end def html_file_path @path ||= Rails.public_path.join(SecureRandom.urlsafe_base64.downcase + ".html") end def screenshot_file_path @screenshot_file_path ||= Rails.public_path.join(SecureRandom.urlsafe_base64.downcase + ".png") end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
html_to-0.7.2 | lib/html_to/html_headless.rb |
html_to-0.7.1 | lib/html_to/html_headless.rb |
html_to-0.7.0 | lib/html_to/html_headless.rb |