Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

In Pyside6 how to maintain HTML table format while printing



  • When I run the below code the HTML table format is not maintained at the time of printing![alt text](image url)

    from PySide6.QtGui import (
        QPageSize,
        QPageLayout,
        QTextBlockFormat,
        QTextCursor,
        QTextDocument,
        QTextFormat,
    )
    from PySide6.QtWidgets import QApplication
    from PySide6.QtPrintSupport import QPrinter, QPrintPreviewDialog
    
    app = QApplication()
    
    dialog = QPrintPreviewDialog()
    
    
    def handle_paint_requested(printer):
        document = QTextDocument()
        cursor = QTextCursor(document)
        blockFormat = QTextBlockFormat()
    
        f = open("template.html", "r")
        billTemplate = f.read()
    
        cursor.insertBlock(blockFormat)
        cursor.insertHtml(billTemplate)
        blockFormat.setPageBreakPolicy(QTextFormat.PageBreak_AlwaysBefore)
        document.print_(printer)
    
    dialog.paintRequested.connect(handle_paint_requested)
    dialog.exec()
    

    Expected
    09764d79-f886-463d-a878-b64a9eb656d0-image.png

    Actual
    784b4d9b-44e2-44ce-9a3e-e5151392c7ca-image.png

    template.html

    <p>&nbsp;</p>
    <table style="border-collapse: collapse; width: 300px;" border="1">
        <tbody>
            <tr>
                <td style="width: 50%;">test1</td>
                <td style="width: 50%;">&nbsp;</td>
            </tr>
        </tbody>
    </table>
    

  • Lifetime Qt Champion

    Hi,

    What if you just use setHtml ?



  • @SGaist
    I tried setHtml it still has the same problem

    from PySide6.QtGui import (
        QPageSize,
        QPageLayout,
        QTextBlockFormat,
        QTextCursor,
        QTextDocument,
        QTextFormat,
    )
    from PySide6.QtWidgets import QApplication
    from PySide6.QtPrintSupport import QPrinter, QPrintPreviewDialog
    
    app = QApplication()
    
    dialog = QPrintPreviewDialog()
    
    
    def handle_paint_requested(printer):
        document = QTextDocument()
        cursor = QTextCursor(document)
        blockFormat = QTextBlockFormat()
    
        f = open("template.html", "r")
        billTemplate = f.read()
    
        document.setHtml(billTemplate)
        document.print_(printer)
    
    
    dialog.paintRequested.connect(handle_paint_requested)
    dialog.exec()
    

  • Lifetime Qt Champion

    Which distribution are you on ?
    Which version of Python ?
    Which version of PySide6 ?
    How did you install both ?



  • @SGaist I am using Ubuntu 20.4, Python version 3.8.10, Pyside6 version 6.1.3. I am using Python virtual environment and installed PySide6 using pip install pyside6

    I also setup the same in Windows with with PySide6 version 6.2 and Python 3.9.7 and same problem. Ubuntu is the primary development environment for me.

    Are you able to run the above code? Are you getting proper result?


  • Lifetime Qt Champion

    QTextDocument does not support full html.

    Try this:

    <p>&nbsp;</p>
    <table width=300 border="1">
        <tbody>
            <tr>
                <td style="width: 50%;">test1</td>
                <td style="width: 50%;">&nbsp;</td>
            </tr>
        </tbody>
    </table>
    

Log in to reply