利用selenium将edge浏览器里面的网页保存为pdf

from msedge.selenium_tools import Edge, EdgeOptions
import json

# Launch Microsoft Edge (Chromium)
options = EdgeOptions()
options.use_chromium = True

settings = {
    "recentDestinations": [{
        "id": "Save as PDF",
        "origin": "local",
        "account": ""
    }],
    "selectedDestinationId": "Save as PDF",
    "version": 2,
    "isHeaderFooterEnabled": False,
    "mediaSize": {
        "height_microns": 297000,
        "name": "ISO_A4",
        "width_microns": 210000,
        "custom_display_name": "A4"
    },
    "customMargins": {},
    "marginsType": 2,
    "isCssBackgroundEnabled": True
}
prefs = {'printing.print_preview_sticky_settings.appState': json.dumps(settings),
         'savefile.default_directory': r'D:\Learn\WeasyPrint'}
options.add_experimental_option('prefs', prefs)
options.add_argument('--kiosk-printing')

driver = Edge(options = options)
driver.get("http://www.ruanyifeng.com/blog")
driver.execute_script('document.title="PDF文件名";window.print();')

driver.quit() 



参考资料:
模拟谷歌浏览器打印pdf功能_半城繁華半城傷的博客-CSDN博客
https://stackoverflow.com/questions/56897041/how-to-save-opened-page-as-pdf-in-selenium-python
posted @ 2021-06-13 19:18  队长  阅读(1665)  评论(0编辑  收藏  举报