Skip to content

Instantly share code, notes, and snippets.

@Satak
Created May 11, 2021 15:31
Show Gist options
  • Save Satak/be5d92d29182851ffbaab2e95a99f59e to your computer and use it in GitHub Desktop.
Save Satak/be5d92d29182851ffbaab2e95a99f59e to your computer and use it in GitHub Desktop.
Get pdf copy of website
from selenium import webdriver
from time import sleep
import json
# download: https://chromedriver.storage.googleapis.com/90.0.4430.24/chromedriver_win32.zip
# extract the content to C:\Program Files\Chromedriver so you have "C:\Program Files\Chromedriver\chromedriver.exe"
# add C:\Program Files\Chromedriver to system environment variable paths
# pip install selenium
URL = "https://google.com"
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),
'savefile.default_directory': 'C:/TEMP'
}
chrome_options.add_experimental_option('prefs', prefs)
chrome_options.add_argument('--kiosk-printing')
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get(URL)
driver.execute_script('window.print();')
sleep(5)
driver.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment