QPrintPreviewDialog - good preview but incorrect printing on Windows 7



  • Hi All,
    I am having a printing problem with PyQt-5.4.1 for Python 3.4.1/Windows 7.
    I want to print an HTML document and the following is my testing code (QWebview and QPrintPreviewDialog):

    import sys, os
    from PyQt5.QtCore import *
    from PyQt5.QtWidgets import *
    from PyQt5 import QtWebKitWidgets
    from PyQt5.QtPrintSupport import QPrintPreviewDialog, QPrinter
    
    class appFormPrintWebviev(QtWebKitWidgets.QWebView):
       def __init__(self, parent=None, ):
              super(appFormPrintWebviev,self).__init__(parent)
             self.loadFinished.connect(self.printPreview)
             self.drawActeNaissance()
    
        def printPreview(self):
    
            dialog = QPrintPreviewDialog(self.printer)
            dialog.setWindowState(Qt.WindowMaximized)
            dialog.paintRequested.connect(self.print_)
            dialog.setWindowFlags(Qt.CustomizeWindowHint | Qt.WindowTitleHint | Qt.WindowMinMaxButtonsHint | Qt.WindowCloseButtonHint | Qt.WindowContextHelpButtonHint)
            dialog.exec()
    
        def drawActeNaissance(self):
            import codecs
            html = codecs.open(b"template.html",encoding='utf-8').read()
            baseurl = QUrl.fromLocalFile(os.path.join(os.getcwd(),"index.html"))
            self.setHtml(html, baseurl)
            self.printer = QPrinter();
            self.printer.setPageSize(QPrinter.A4);
            self.printer.setOrientation(QPrinter.Portrait);
            self.printer.setPageMargins(10,10,10,10,QPrinter.Millimeter);
            self.setFixedWidth(1000)
    
    if __name__ == '__main__':
            app = QApplication(sys.argv)
            form = appFormPrintWebviev()
            form.show()
            sys.exit(app.exec_())
    

    My problem is that the QPrintPreviewDialog correctly displays the document in previewing window (see: http://s1.postimg.org/fc4ctwian/Print_Preview.png), but the document is not correctly printed. The printer, PDFCreator, prints a disorganized document (see: http://s15.postimg.org/kj940hb23/final_printed_doc.png)

    I don't know why, but the same code with adaptations works perfectly in PyQt-4.10.4 and Python 2.7.

    Thieré
    Regards

    PS : I have already posted this message to PyQT list but I havd no answer.



  • After a looking around Qt BugTracker
    it seams that it is a known problem from QT 5.0.1 which has desapeared in version 5.3 and reappeared in 5.4.
    https://bugreports.qt.io/browse/QTBUG-36308
    But it is not solved yet I think for official version of Qt 5.4.1 https://bugreports.qt.io/browse/QTBUG-43145
    Even if patch seems to be available https://codereview.qt-project.org/#/c/111658/



  • I solved I by moving from PyQT5.4.1 to PyQT5.3.2.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.