Unsolved 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
Actual
template.html
<p> </p> <table style="border-collapse: collapse; width: 300px;" border="1"> <tbody> <tr> <td style="width: 50%;">test1</td> <td style="width: 50%;"> </td> </tr> </tbody> </table>
-
Hi,
What if you just use setHtml ?
-
@SGaist
I tried setHtml it still has the same problemfrom 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()
-
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?
-
QTextDocument does not support full html.
Try this:
<p> </p> <table width=300 border="1"> <tbody> <tr> <td style="width: 50%;">test1</td> <td style="width: 50%;"> </td> </tr> </tbody> </table>