我正在尝试使用 Selenium、chromedriver 和 python 实现将 html/css 内容打印为 PDF。
我可以使用以下代码进行打印,但无法更改打印设置。我想以 Letter 尺寸打印,并且没有页眉/页脚。官方信息chromedriver或Selenium并没有告诉我很多信息,所以我遇到了麻烦。有谁知道如何更改打印设置或永远无法完成。
import json
import os
from selenium import webdriver
# setting html path
htmlPath = os.getcwd() + "\\sample.html"
addr = "file:///" + htmlPath
# setting Chrome Driver
chromeOpt = webdriver.ChromeOptions()
appState = {
"recentDestinations": [
{
"id": "Save as PDF",
"origin": "local",
"account": ""
}
],
"selectedDestinationId": "Save as PDF",
"version": 2
}
prefs = {
'printing.print_preview_sticky_settings.appState': json.dumps(appState)}
chromeOpt.add_experimental_option('prefs', prefs)
chromeOpt.add_argument('--kiosk-printing')
driver = webdriver.Chrome('.\\bin\\chromedriver', options=chromeOpt)
# …Run Code Online (Sandbox Code Playgroud)