相关疑难解决方法(0)

使用 chromedriver 从 Selenium 打印 PDF

我正在尝试使用 Selenium、chromedriver 和 python 实现将 html/css 内容打印为 PDF。

我可以使用以下代码进行打印,但无法更改打印设置。我想以 Letter 尺寸打印,并且没有页眉/页脚。官方信息chromedriverSelenium并没有告诉我很多信息,所以我遇到了麻烦。有谁知道如何更改打印设置或永远无法完成。

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)

python selenium selenium-chromedriver

2
推荐指数
1
解决办法
6035
查看次数

标签 统计

python ×1

selenium ×1

selenium-chromedriver ×1