0

我成功地将 HTML 转换为 PDF(或打印为 pdf)。但是,Cookies 和隐私政策声明印在每页的底部。如何删除此通知或接受 cookie?(任何网站域的解决方案)

在此处输入图像描述

import os
import sys
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets


def html_to_pdf(html, pdf):
    app = QtWidgets.QApplication(sys.argv)

    page = QtWebEngineWidgets.QWebEnginePage()

    def handle_print_finished(filename, status):
        print("finished", filename, status)
        QtWidgets.QApplication.quit()

    def handle_load_finished(status):
        if status:
            page.printToPdf(pdf)
        else:
            print("Failed")
            QtWidgets.QApplication.quit()

    page.pdfPrintingFinished.connect(handle_print_finished)
    page.loadFinished.connect(handle_load_finished)
    page.setUrl(QtCore.QUrl(html))
    app.exec_()


if __name__ == "__main__":
    html_to_pdf("https://stackoverflow.com/questions/2530/how-do-you-disable-browser-autocomplete-on-web-form-field-input-tag?rq=1", "test.pdf")
4

1 回答 1

2

一种可能的解决方案是实现一个单击按钮的 Js 脚本,如下例所示:

import os
import sys
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets


def html_to_pdf(html, pdf):
    app = QtWidgets.QApplication(sys.argv)

    page = QtWebEngineWidgets.QWebEnginePage()

    def handle_print_finished(filename, status):
        print("finished", filename, status)
        QtWidgets.QApplication.quit()

    def handle_load_finished(status):
        if status:
            execute_js()
        else:
            print("Failed")
            QtWidgets.QApplication.quit()

    def handle_run_js(status):
        if status:
            QtCore.QTimer.singleShot(1000, print_pdf)
        else:
            QtCore.QTimer.singleShot(1000, execute_js)

    def execute_js():
        page.runJavaScript(
            """
            (function () {
                var elements = document.getElementsByClassName("js-consent-banner-hide")
                if(elements.length > 0){
                    elements[0].click()
                    return true;
                }
                return false;
            })();
            """,
            handle_run_js,
        )

    def print_pdf():
        page.printToPdf(pdf)

    page.pdfPrintingFinished.connect(handle_print_finished)
    page.loadFinished.connect(handle_load_finished)
    page.setUrl(QtCore.QUrl(html))
    app.exec_()


if __name__ == "__main__":
    html_to_pdf(
        "https://stackoverflow.com/questions/2530/how-do-you-disable-browser-autocomplete-on-web-form-field-input-tag?rq=1",
        "test.pdf",
    )
于 2021-07-13T15:18:47.373 回答