0

I have a python code like below

import sys
from PyQt5 import QtWidgets, QtWebEngineWidgets
from PyQt5.QtNetwork import QNetworkCookie
from PyQt5.QtCore import QUrl, QTimer
from PyQt5.QtGui import QPageLayout, QPageSize
from PyQt5.QtWidgets import QApplication
import argparse

def main(app):
    url = ''
    parser = argparse.ArgumentParser(description="Just an example", formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument("--url", help="Type url")
    args = parser.parse_args()
    config = vars(args)
    url = config['url']

    loader = QtWebEngineWidgets.QWebEngineView()
    profile = QtWebEngineWidgets.QWebEngineProfile("storage", loader)
    cookie_store = profile.cookieStore()

    with open('cookie.txt', 'rb') as f:
        contents = f.read()

    cookie_store.setCookie(QNetworkCookie(contents))
    webpage = QtWebEngineWidgets.QWebEnginePage(profile, loader)
    COOKIPATH = "./Cache" + str(1)
    # web_profile.setCachePath(COOKIPATH)
    profile.setPersistentStoragePath(COOKIPATH)
    profile.setPersistentCookiesPolicy(2)
    loader.setPage(webpage)
    loader.setZoomFactor(1)
    layout = QPageLayout()
    layout.setPageSize(QPageSize(QPageSize.A4Extra))
    layout.setOrientation(QPageLayout.Portrait)
    loader.load(QUrl(url))
    loader.page().pdfPrintingFinished.connect(lambda *args: QApplication.exit())

    def emit_pdf(finished):
        QTimer.singleShot(2000, lambda: loader.page().printToPdf("test.pdf", pageLayout=layout))


    loader.loadFinished.connect(emit_pdf)
    app.exec()


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    main(app)

I run above code like this python3 htmlToPdfnew.py --url http://example.com

but when I run the code I get a warning like below

Release of profile requested but WebEnginePage still not deleted. Expect troubles !

based on suggestions I got from internet calling app.exec()

but still get that error

how to resolve that error ?

AMendis
  • 1,346
  • 4
  • 18
  • 34
  • You get that "error", but, most importantly, does the program run successfully? – musicamante May 13 '22 at 03:18
  • @musicamante yes. I get the correct output – AMendis May 13 '22 at 04:09
  • Then, you probably don't need to worry to much. Besides, try removing the `main()` entirely, and move everything in the `if __name__` block, which might solve some context issues related to that warning. – musicamante May 13 '22 at 16:24

0 Answers0