29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
# File 'app/models/social_construct/base_card.rb', line 29
def to_png
log_debug("Starting PNG generation for #{self.class.name}")
browser_options = {
headless: true,
timeout: 30,
window_size: [width, height]
}
if Rails.env.production? || ENV["DOCKER_CONTAINER"].present?
browser_options[:browser_options] = {
:"no-sandbox" => nil,
:"disable-dev-shm-usage" => nil,
:"disable-gpu" => nil,
:"disable-software-rasterizer" => nil,
:"disable-web-security" => nil,
:"force-color-profile" => "srgb"
}
log_debug("Using production browser options")
end
browser = Ferrum::Browser.new(browser_options)
html_content = render
log_debug("HTML content length: #{html_content.length} bytes")
log_debug("HTML encoding: #{html_content.encoding}")
if html_content.length > 1_000_000
log_debug("HTML too large for data URL, using temp file")
require "tempfile"
temp_file = Tempfile.new(["social_card", ".html"])
temp_file.write(html_content)
temp_file.rewind
temp_file.close
begin
browser.goto("file://#{temp_file.path}")
ensure
temp_file.unlink
end
else
html_content = html_content.force_encoding("UTF-8")
encoded_html = ERB::Util.url_encode(html_content)
browser.goto("data:text/html;charset=utf-8,#{encoded_html}")
end
browser.set_viewport(width: width, height: height)
browser.network.wait_for_idle
browser.execute(
" return new Promise((resolve) => {\n // Check if all images are loaded\n const images = Array.from(document.querySelectorAll('img'));\n const imagePromises = images.map(img => {\n if (img.complete) return Promise.resolve();\n return new Promise(res => {\n img.addEventListener('load', res);\n img.addEventListener('error', res);\n });\n });\n\n // Check document fonts\n const fontPromise = document.fonts?.ready || Promise.resolve();\n\n // Wait for everything\n Promise.all([...imagePromises, fontPromise]).then(() => {\n // Small delay to ensure rendering is complete\n requestAnimationFrame(() => {\n setTimeout(resolve, 50);\n });\n });\n });\n JS\n )\n\n # Log page readiness\n if debug\n page_ready = browser.evaluate(\"document.readyState\")\n log_debug(\"Page ready state: \#{page_ready}\")\n\n # Log computed styles to check if CSS is applied\n body_bg = browser.evaluate(\"window.getComputedStyle(document.body).backgroundColor\")\n log_debug(\"Body background color: \#{body_bg}\")\n\n # Check if content is visible\n has_title = browser.evaluate(\"!!document.querySelector('.title')\")\n title_text = browser.evaluate(\"document.querySelector('.title')?.textContent\") if has_title\n log_debug(\"Has title element: \#{has_title}, Title text: \#{title_text}\")\n\n # Check HTML body content\n body_html_length = browser.evaluate(\"document.body.innerHTML.length\")\n log_debug(\"Body HTML length: \#{body_html_length}\")\n end\n\n # Ensure content is painted before screenshot\n browser.execute(\n <<~JS\n // Force layout and paint\n document.body.offsetHeight;\n // Check if we have visible content\n const hasContent = document.body.textContent.trim().length > 0 ||\n document.querySelectorAll('img').length > 0;\n if (!hasContent) {\n console.warn('Page appears to have no visible content');\n }\n return hasContent;\n JS\n )\n\n screenshot = browser.screenshot(\n encoding: :binary,\n quality: 100,\n full: false\n )\n\n log_debug(\"Screenshot generated, size: \#{screenshot.bytesize} bytes\")\n\n screenshot\nrescue => e\n log_debug(\"Ferrum screenshot failed: \#{e.message}\", :error)\n log_debug(\"Backtrace: \#{e.backtrace.first(5).join(\"\\n\")}\", :error) if debug\n raise\nensure\n browser&.quit\nend\n"
|