0

I'm successfully converting HTML to PDF (or print to pdf). However, Cookies and Privacy Policy notices are printed at the bottom of each page. How do I remove this notice or accept cookies? (solutions for any website domain)

enter image description here

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")

1 Answers1

2

A possible solution is to implement a Js script that clicks the button like the following example:

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",
    )
eyllanesc
  • 221,139
  • 17
  • 121
  • 189
  • This solution works, but only for the StackOverflow domain. It is partially useful for me, thanks! Any idea for a generic solution for any website? Can QtWebEngineWidgets accept/ignore all cookies automatically? – Alexandre Strapacao G. Vianna Jul 13 '21 at 15:32
  • @AlexandreStrapacaoG.Vianna There is no generic method since for security reasons you must click on the button. If you find a button in class or name of the buttons then you could apply my solution. – eyllanesc Jul 13 '21 at 15:37
  • Something is wrong, Now I'm getting this error msg: js: A cookie associated with a cross-site resource at http://yahoo.com/ was set without the `SameSite` attribute. A future release of Chrome will only deliver cookies with cross-site requests if they are set with `SameSite=None` and `Secure`. You can review cookies in developer tools under Application>Storage>Cookies and see more details at https://www.chromestatus.com/feature/5088147346030592 and https://www.chromestatus.com/feature/5633521622188032. js: A cookie associated with a cross-site resource at http://everesttech.net/ was set w... – Alexandre Strapacao G. Vianna Jul 13 '21 at 18:51