Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. In Pyside6 how to maintain HTML table format while printing
Forum Updated to NodeBB v4.3 + New Features

In Pyside6 how to maintain HTML table format while printing

Scheduled Pinned Locked Moved Unsolved Qt for Python
6 Posts 2 Posters 701 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kumar80
    wrote on last edited by
    #1

    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>
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What if you just use setHtml ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      K 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        What if you just use setHtml ?

        K Offline
        K Offline
        kumar80
        wrote on last edited by
        #3

        @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()
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          K 1 Reply Last reply
          0
          • SGaistS SGaist

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

            K Offline
            K Offline
            kumar80
            wrote on last edited by
            #5

            @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?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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>
              

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved