完整代码 Page.printToPDF

说明: 我这里用的是Python3.12.3, selenium4.0版本

import base64
import json
import os
import time

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service


def send_devtools(driver, cmd, params={}):
    resource = "/session/%s/chromium/send_command_and_get_result" % driver.session_id
    url = driver.command_executor._url + resource
    body = json.dumps({'cmd': cmd, 'params': params})
    response = driver.command_executor._request('POST', url, body)
    return response.get('value')


def save_as_pdf(driver, path, options={}):
    result = send_devtools(driver, "Page.printToPDF", options)
    with open(path, 'wb') as file:
        file.write(base64.b64decode(result['data']))


if __name__ == "__main__":
    # Chrome浏览器以及chromedriver的存放文件夹,注意chrome浏览器和chromedriver的版本号要保持同一个版本,不然会报版本错误
    chrome_location = os.path.join(os.getcwd(), 'chrome-win')
    # Chrome浏览器的位置
    browser_location = os.path.join(chrome_location, 'chrome.exe')
    # ChromeDriver的位置
    driver_location = os.path.join(chrome_location, 'chromedriver.exe')

    # linux上 chrome和chromedriver
    #chrome_location = os.path.join(current_path, 'chrome-linux')
    #browser_location = os.path.join(chrome_location, 'chrome')
    #driver_location = os.path.join(chrome_location, 'chromedriver')
    #print('Linux上ChromeDriver的位置:', driver_location)



    # 创建一个Servic对象,传入ChromeDriver的路径
    service = Service(driver_location)
    # 创建Chrome选项
    options = Options()
    # 无头模式,无界面
    options.add_argument("--headless")
    options.add_argument("--disable-gpu")

    driver = webdriver.Chrome(options, service)
    try:
        driver.get("https://www.baidu.com")
        time.sleep(3)  # 如果页面复杂,非静态页面,建议适当给延迟,等待页面彻底加载完成
        # 创建存放pdf文件夹
        prefix_path = os.path.join(os.getcwd(), 'pdf')
        if not os.path.exists(prefix_path):
            os.makedirs(prefix_path)
        # 完成的pdf的完整路径,可以根据自己的需求自己设置文件路径、文件名
        save_path = os.path.join(prefix_path, 'my.pdf')
        print(save_path)
        # landscape 布局: true:横向,false:纵向,默认值
        # pageRanges 页面 默认空,代表打印全部,也可以打印部分也例如: '1-5'
        # printBackground 打印背景, true:打印背景,false: 不打印背景 默认是false
        save_as_pdf(driver, save_path,
                    {'landscape': True, 'pageRanges': '', 'printBackground': True})
    except Exception as e:
        print(e)
    finally:
        driver.quit()

亲测Linux环境可运行

因为需要部署linxu环境,安装完Python环境和chrome浏览器

注意Linux上的chromedriver也要和chrome浏览器版本一致

本人亲测,Linux上selenium chrome浏览器无头模式 ,html转换(打印)为pdf完全可以

参考资料 https://www.cnblogs.com/new-june/p/15347577.html 

               https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-printToPDF

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐