셀레니움으로 여러 웹페이지를 자동으로 pdf로 출력되게 하고 있는데
일단 기능적으로는 원하는대로 다 된거 같음..
그런데 각 웹페이지를 pdf로 출력할때마다 위 스샷처럼
크롬드라이버 터미널? 이 실행되었다가 꺼졌다가 하는데 이거 어떻게 못함?
웹페이지 100개 pdf로 만들면 터미널이 100번 켜졌다 꺼졌다 하니까 좀 거슬림;
밑에 코드 남기니 좀 알려주세요
def WebToPDF(url,file_name):
DRIVER_PATH = 'chromedriver.exe'
# headless 모드 실행
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox')
driver = webdriver.Chrome(DRIVER_PATH, options=options)
driver.get(url)
# 웹페이지 스크롤(수평스크롤, 수직스크롤), 사이즈 조정
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
width = driver.execute_script("return Math.max(document.body.scrollWidth, document.body.offsetWidth, document.documentElement.clientWidth, document.documentElement.scrollWidth, document.documentElement.offsetWidth);")
height = driver.execute_script("return Math.max(document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight);")
driver.set_window_size(width, height)
# 스크린샷 저장
png = driver.get_screenshot_as_png()
im = Image.open(io.BytesIO(png)).convert('RGB')
im.save(os.path.join(txt_frame1_path.get(), file_name + '.png'))
# 스크린샷 이미지를 A4 크기로 나누어 PDF 파일 생성
scale = 5
a4_width = int(297 * scale)
a4_height = int(420 * scale)
im = Image.open(os.path.join(txt_frame1_path.get(), file_name + '.png'))
im_width, im_height = im.size
pages = []
for y in range(0, im_height, a4_height):
for x in range(0, im_width, a4_width):
box = (x, y, x+a4_width, y+a4_height)
pages.append(im.crop(box))
pdf_path = os.path.join(txt_frame1_path.get(), file_name + '.pdf')
pages[0].save(pdf_path, save_all=True, append_images=pages[1:])
driver.quit()
os.remove(os.path.join(txt_frame1_path.get(), file_name + '.png'))
댓글 1