Last active
October 4, 2019 16:56
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import time | |
from pyvirtualdisplay import Display | |
from selenium import webdriver | |
document_url = "https://www.adobe.com/content/dam/acom/en/accessibility/products/acrobat/pdfs/acrobat-x-accessibility-checker.pdf" | |
download_dir = "/path/to/dir/" | |
# setup a virtual display using pyvirtualdisplay | |
display = Display(visible=0, size=(1768, 1368)) | |
display.start() | |
chrome_options = webdriver.ChromeOptions() | |
chrome_options.add_argument("--no-sandbox") | |
chrome_options.add_argument("--disable-notifications") | |
chrome_options.add_argument("--disable-popup-blocking") | |
chrome_options.add_argument("--disable-logging") | |
chrome_options.add_argument("--log-level=3") | |
# chrome option settings to enable automatic download in the specified folder without showing Save As PDF dialog box. | |
chrome_options.add_argument("--kiosk-printing") | |
appState = { | |
"recentDestinations": [{"id": "Save as PDF", "origin": "local"}], | |
"selectedDestinationId": "Save as PDF", | |
"version": 2, | |
} | |
prefs = { | |
"printing.print_preview_sticky_settings.appState": json.dumps(appState), | |
"download": { | |
"default_directory": download_dir, | |
"prompt_for_download": False, | |
"directory_upgrade": True, | |
}, | |
} | |
chrome_options.add_experimental_option("prefs", prefs) | |
driver = webdriver.Chrome(chrome_options=chrome_options) | |
driver.get(document_url) | |
time.sleep(10) | |
driver.execute_script("window.print();") | |
time.sleep(30) | |
driver.quit() | |
display.stop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment