I have a python code which convert url to pdf. My python code is below
import sys
from PyQt6 import QtWidgets, QtWebEngineWidgets
from PyQt6.QtCore import QUrl, QTimer, QDateTime, Qt
from PyQt6.QtGui import QPageLayout, QPageSize
from PyQt6.QtWidgets import QApplication
from PyQt6.QtNetwork import QNetworkCookie
import argparse
import os
import json
class Window(QtWebEngineWidgets.QWebEngineView):
def __init__(self, *args, **kwargs):
super(Window, self).__init__(*args, **kwargs)
self.cookieStore = self.page().profile().cookieStore()
def initCookies(self, cookie_file):
if cookie_file:
print("Cookie parameter specified " + cookie_file)
with open("output/" + cookie_file, encoding='utf8') as f:
cookies = json.load(f)
for cookie in cookies:
qcookie = QNetworkCookie()
qcookie.setName(cookie.get('name', '').encode())
qcookie.setValue(cookie.get('value', '').encode())
qcookie.setDomain(cookie.get('domain', ''))
qcookie.setPath(cookie.get('path', ''))
qcookie.setExpirationDate(
QDateTime.fromString(str(cookie.get('expirationDate', 0)), Qt.DateFormat.ISODate))
qcookie.setHttpOnly(cookie.get('httpOnly', False))
qcookie.setSecure(cookie.get('secure', False))
self.cookieStore.setCookie(qcookie, QUrl())
def main():
file_name = 'ABC123.pdf'
cookie_file = None
parser = argparse.ArgumentParser(description="Just an example",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("--url", help="Type url", required=True)
parser.add_argument("--output", help="Type output pdf file name")
parser.add_argument("--cookie", help="Type cookie file name")
parser.add_argument("--orientation", help="Type Orientation Portrait or Landscape")
args = parser.parse_args()
config = vars(args)
url = config['url']
output = config['output']
cookie = config['cookie']
orientation = config['orientation']
if output:
file_name = output
if cookie:
cookie_file = cookie
print(url);
app = QtWidgets.QApplication(sys.argv)
loader = Window()
if cookie_file:
loader.initCookies(cookie_file)
loader.setZoomFactor(1)
layout = QPageLayout()
layout.setPageSize(QPageSize(QPageSize.PageSizeId.A4))
if orientation == 'Landscape':
layout.setOrientation(QPageLayout.Orientation.Landscape)
else:
layout.setOrientation(QPageLayout.Orientation.Portrait)
loader.load(QUrl(url))
loader.page().pdfPrintingFinished.connect(lambda *args: QApplication.exit())
def emit_pdf():
print("Hi........Hi")
directory = "output/"
if not os.path.exists(directory):
os.makedirs(directory)
QTimer.singleShot(2000, lambda: loader.page().printToPdf(directory + file_name, pageLayout=layout))
def generate_pdf():
# Wait for 5 seconds after the page has finished loading before generating the PDF
QTimer.singleShot(5000, emit_pdf)
loader.loadFinished.connect(generate_pdf)
sys.exit(app.exit())
if __name__ == '__main__':
main()
I have created a folder called "output" and put mycookie.txt there. This is how I run my code
export QT_QPA_PLATFORM=offscreen
python3 htmlToPdfnew.py --url https://example.com --output testnew.pdf --cookie mycookie.txt
I don't see any errors. But I don't see testnew.pdf in my output folder. So I did a debug and found generate_pdf() is NOT getting executed
What am I doing wrong ? Why testnew.pdf is NOT getting generated ?