在Chrome中以pdf格式自动将网页打印/保存为pdf-python 2.7 [英] Automate print/save web page as pdf in chrome - python 2.7

查看:170
本文介绍了在Chrome中以pdf格式自动将网页打印/保存为pdf-python 2.7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在chrome中自动将打印保存网页"以pdf格式保存.

I am trying to automate Print Save Web Page as pdf in chrome.

我已经检查了webbrowser模块,但似乎不打算用于此目的.

I have checked the webbrowser module, but it does not seem to be intended for this purpose.

我探索了wkhtmltopdf作为替代方法,但是下载文件时,它似乎已被病毒感染.

I explored wkhtmltopdf as an alternative but when downloading the file it seems to be infected by a virus.

谢谢你的建议.

推荐答案

在Windows 7上,我使用Chrome 62.0.3202.94,ChromeDriver 2.33.506120,Selenium 3.4.3和Python 2.7.14或3.6.3为我工作x64:

This worked for me using Chrome 62.0.3202.94, ChromeDriver 2.33.506120, Selenium 3.4.3, and Python 2.7.14 or 3.6.3, on Windows 7 x64:

import json
from selenium import webdriver

appState = {
    "recentDestinations": [
        {
            "id": "Save as PDF",
            "origin": "local",
            "account": ""
        }
    ],
    "selectedDestinationId": "Save as PDF",
    "version": 2
}

profile = {'printing.print_preview_sticky_settings.appState': json.dumps(appState)}

chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option('prefs', profile)
chrome_options.add_argument('--kiosk-printing')

driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get('https://www.google.com/')
driver.execute_script('window.print();')

这篇关于在Chrome中以pdf格式自动将网页打印/保存为pdf-python 2.7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆