Sha256: 11b3d46ba39c22702809b5265f4a33b03288881903f05dee1ac4dc95df7a11ae
Contents?: true
Size: 1.79 KB
Versions: 1
Compression:
Stored size: 1.79 KB
Contents
class HtmlTo::HtmlHeadless require 'fileutils' 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 chrome if RbConfig::CONFIG['host_os'] =~ /darwin/ "/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome" elsif RbConfig::CONFIG['host_os'] =~ /linux/ release = IO.popen("lsb_release -i -s").read if release == "Debian\n" 'chromium' else 'chromium-browser' end else raise StandardError.new "host os don't detected" 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
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
html_to-0.0.2 | lib/html_to/html_headless.rb |