I want to use Python to convert some html files to PDF. After looking at the modules and possibilities to do this, I settled on Qt5.
I have code that works, but it doesn't work exactly how I want. I want to run my program via the command line, and it to exit once finished. Currently, it produces the PDF but stays running.
If I uncomment and show the QWebEngineView with view.show(), then I can close the window that opens and the program will exit. (But I don't want to have any GUI, so this isn't good)
Next, I tried to show the view, then immediately close it after with view.close(), but when I do this a PDF doesn't get generated. I tried adding a 5 second sleep after view.show() and before view.close(), but still the same result.
I'm not sure if what I want is possible, I understand Qt is for GUIs but it would be very convenient if I could get this working completely through the command line.
import sys import os from time import sleep from PyQt5.QtWidgets import QApplication from PyQt5.QtWebEngineWidgets import QWebEngineView from PyQt5.QtCore import QDir, QUrl # needed to load local html files sys.argv.append("--disable-web-security") app = QApplication(sys.argv) raw_html = "" with open('webpage.htm', 'r') as myfile: raw_html = myfile.read() view = QWebEngineView() view.setHtml(raw_html) def save_pdf(finished): view.page().printToPdf("output.pdf") # view.show() # view.close() view.loadFinished.connect(save_pdf) sys.exit(app.exec_())