tomo2chin2 commited on
Commit
803f7c6
·
verified ·
1 Parent(s): f1bfdc2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -14
app.py CHANGED
@@ -10,6 +10,21 @@ import tempfile
10
  import time
11
  import os
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  def render_fullpage_screenshot(html_code):
14
  # HTMLコードを一時ファイルに保存
15
  tmp_file = tempfile.NamedTemporaryFile(suffix=".html", delete=False)
@@ -27,7 +42,7 @@ def render_fullpage_screenshot(html_code):
27
  try:
28
  driver = webdriver.Chrome(options=options)
29
 
30
- # まずは適当なウィンドウサイズでページを読み込む
31
  driver.set_window_size(1200, 800)
32
  driver.get("file://" + tmp_path)
33
 
@@ -35,30 +50,30 @@ def render_fullpage_screenshot(html_code):
35
  WebDriverWait(driver, 10).until(
36
  EC.presence_of_element_located((By.TAG_NAME, "body"))
37
  )
38
- # フォントや外部リソース読み込みの安定化のため少し待機
39
- time.sleep(2)
40
 
41
- # スクロールバーを非表示にする(必要に応じてコメントアウト)
42
  driver.execute_script("""
43
  document.documentElement.style.overflow = 'hidden';
44
  document.body.style.overflow = 'hidden';
45
  """)
46
 
47
- # ページ全体の幅・高さを取得
48
- scroll_width = driver.execute_script("return document.body.scrollWidth")
49
- scroll_height = driver.execute_script("return document.body.scrollHeight")
50
-
51
- # ウィンドウサイズをページ全体の幅・高さに再設定
52
- driver.set_window_size(scroll_width, scroll_height)
53
 
54
- # レイアウト変化を待つ
55
- time.sleep(2)
 
 
 
56
 
57
- # ページ全体をスクリーンショット
58
  png = driver.get_screenshot_as_png()
59
 
60
  except Exception as e:
61
- # 何らかのエラー時は1x1の黒画像を返す
62
  return Image.new('RGB', (1, 1), color=(0, 0, 0))
63
 
64
  finally:
 
10
  import time
11
  import os
12
 
13
+ def get_full_dimensions(driver):
14
+ """
15
+ body.scrollWidth / scrollHeight と
16
+ documentElement.scrollWidth / scrollHeight を比較して
17
+ ページ全体の幅・高さを返すユーティリティ関数
18
+ """
19
+ body_w = driver.execute_script("return document.body.scrollWidth")
20
+ body_h = driver.execute_script("return document.body.scrollHeight")
21
+ html_w = driver.execute_script("return document.documentElement.scrollWidth")
22
+ html_h = driver.execute_script("return document.documentElement.scrollHeight")
23
+
24
+ full_width = max(body_w, html_w)
25
+ full_height = max(body_h, html_h)
26
+ return full_width, full_height
27
+
28
  def render_fullpage_screenshot(html_code):
29
  # HTMLコードを一時ファイルに保存
30
  tmp_file = tempfile.NamedTemporaryFile(suffix=".html", delete=False)
 
42
  try:
43
  driver = webdriver.Chrome(options=options)
44
 
45
+ # 1) とりあえず適当なウィンドウサイズでページを読み込む
46
  driver.set_window_size(1200, 800)
47
  driver.get("file://" + tmp_path)
48
 
 
50
  WebDriverWait(driver, 10).until(
51
  EC.presence_of_element_located((By.TAG_NAME, "body"))
52
  )
53
+ time.sleep(1) # 追加待機
 
54
 
55
+ # 2) スクロールバーを非表示にする(必要に応じてコメントアウト)
56
  driver.execute_script("""
57
  document.documentElement.style.overflow = 'hidden';
58
  document.body.style.overflow = 'hidden';
59
  """)
60
 
61
+ # 3) ページ全体のサイズを取得してウィンドウサイズを拡張
62
+ full_width, full_height = get_full_dimensions(driver)
63
+ driver.set_window_size(full_width, full_height)
64
+ time.sleep(2) # レイアウト変化待ち
 
 
65
 
66
+ # 4) もう一度ページ全体のサイズを取得し、もし変化していれば再調整
67
+ new_full_width, new_full_height = get_full_dimensions(driver)
68
+ if (new_full_width != full_width) or (new_full_height != full_height):
69
+ driver.set_window_size(new_full_width, new_full_height)
70
+ time.sleep(2) # もう一度待機
71
 
72
+ # 5) ページ全体をスクリーンショット
73
  png = driver.get_screenshot_as_png()
74
 
75
  except Exception as e:
76
+ # エラー時は1x1の黒画像を返す
77
  return Image.new('RGB', (1, 1), color=(0, 0, 0))
78
 
79
  finally: