Python

【Python】SeleniumでHTMLをPDF出力して保存する

この記事は約3分で読めます。

 

SeleniumでHTMLをPDF出力して保存する

 

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.alert import Alert
import json
import time
from webdriver_manager.chrome import ChromeDriverManager 
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import chromedriver_binary 
import subprocess


cmd = 'pip install --upgrade chromedriver_binary' 
res = subprocess.call(cmd, shell=False) 



chrome_options = webdriver.ChromeOptions()
settings = {
       "recentDestinations": [{
            "id": "Save as PDF",
            "origin": "local",
            "account": ""
        }],
        "selectedDestinationId": "Save as PDF",
        "version": 2
    }
prefs = {'printing.print_preview_sticky_settings.appState': json.dumps(settings)}
chrome_options.add_experimental_option('prefs', prefs)
chrome_options.add_argument('--kiosk-printing')

driver = webdriver.Chrome(ChromeDriverManager().install(), chrome_options=chrome_options)
driver.get("https://tkstock.site/")
driver.execute_script('return window.print()')
driver.quit()

 

 

 

 

 

参考:https://qiita.com/kuro227/items/838b9bab231902e280d4

参考:https://www.fixes.pub/program/510191.html

 

 

コメント

タイトルとURLをコピーしました