How to print to a printer PySide6
-
I am starting off with Pyside6 and trying to build a billing application which runs on ubuntu. How do format and print the bill any sample or suggestions will be helpful
@kumar80 Demo:
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() for i in range(1, 11): cursor.insertBlock(blockFormat) cursor.insertHtml(f"<h1>This is the {i} page</h1>") blockFormat.setPageBreakPolicy(QTextFormat.PageBreak_AlwaysBefore) document.print_(printer) dialog.paintRequested.connect(handle_paint_requested) dialog.exec() -
@eyllanesc said in How to print to a printer PySide6:
filename = "test.pdf"
Do we need to create a pdf file before printing it? For my use case will be filling out a form with text boxes and then click on print. In such case is there a way to print without have having an intermediate pdf?
-
@eyllanesc said in How to print to a printer PySide6:
filename = "test.pdf"
Do we need to create a pdf file before printing it? For my use case will be filling out a form with text boxes and then click on print. In such case is there a way to print without have having an intermediate pdf?
-
Sorry to revive this topic but I can't deal with it. For me, the text on the first page is not displayed in the dialog box, but it prints correctly. Why? How to improve it?
-
My post is about the code written by eylanesc in this thread.
This is what Print Preview looks like:

This is what the PDF document looks like after printing:

Print Preview is missing text on the first page but prints correctly. I don't know why this happens, can it be improved?