Using Pyside6, I am trying to print an HTML Table. The HTML table format looks good in browser. However, when I open it up in print preview in Pyside6, the length and width of the table are not considered and the table collapses.
Devlopment Environment:
- Ubuntu 20.04
- Python 3.8.10 (using python virtual environment)
- PySide6 version 6.1.3
I also tried the same code in Windows with Python 3.9.7 and Pyside6 version 6.2, and got the same result.
Expected:
Actual:
main.py
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()
f = open("template.html", "r")
billTemplate = f.read()
document.setHtml(billTemplate)
document.print_(printer)
dialog.paintRequested.connect(handle_paint_requested)
dialog.exec()
template.html
<p> </p>
<table style="border-collapse: collapse; width: 300px;" border="1">
<tbody>
<tr>
<td style="width: 50%; height: 50px;">test1</td>
<td style="width: 50%; height: 50px;"> </td>
</tr>
</tbody>
</table>


Since QTextDocument only supports a subset of HTML4 properties it causes the observed error.
One workaround is to use QtWebEngine which was reintroduced in Qt 6.2. Currently only the windows .whl is available in pypi (See here and here for more information) so we can install the Linux and MacOs package you must execute:
So translating my previous answer to PySide6:
After pressing Ctrl + P you get: