Search code examples
pythonprintingpyqt5qwebengineviewqprintpreviewdialog

QWebEnginePage.print with QPrintPreviewDialog results in empty preview


I have a program that used to use QWebKit to show and print custom generated HTML reports in a dialog and now, I want to convert the whole thing to QWebEngine. Everything works fine so far, only printing doesn't!

Up to now I used QWebView.print() to hand the whole HTML data over to a QPrintPreviewDialog which wasn't a problem at all. Now, from what I understand, I thought I'd have to use QWebEngineView.QWebEnginepage.print(printer object, callback) to achieve the same.

Sadly, everything I tried so far hasn't worked. The preview pane of the QPrintPreviewDialog always stays empty and the result which is given back to the callback after printing is done is always False - although the printer object is still valid the whole time, even in the callback.

I tried out different things, which worked somehow and therefor I think, the QWebEnginePage.print() function is buggy somehow or there is a bug in PyQt, but in fact, I don't and perhaps it is me doing something wrong.

Successful workarounds (which I don't like ;-) ):

  1. Don't use QPrintPreviewDialog, but generate the output via QWebEngineView.QWebEnginePage.printToPDF(filename): worked!

  2. Generate a temporary QTextedit object and print this via QPrintPreviewDialog: also worked, but badly formatted HTML in my case...

Can someone tell me, if I'm assuming something wrong here or if there is a descent example on how to print HTML via QPrintPreviewDialog using QWebEngineView?


Solution

  • QtWebEngine currently doesn't support printing to a print preview, see QTBUG-57982. Printing to a QPrinter directly works though.