Qprinter Extra margin at left Side

79 Views Asked by At

I am using pyqt5 I used QTextDocument and tried to print a Text but i am Getting margin on left side. I tried everything to remove the margin but it is still not going away. I used setPageMargins and setDocumentMargin but still getting that margin on left side.

Here is the Code:

from PyQt5.QtWidgets import QApplication, QMainWindow, QTextEdit, QVBoxLayout, QPushButton, QWidget
from PyQt5.QtGui import QTextDocument, QTextCursor, QTextCharFormat,QTextFrameFormat,QTextBlockFormat
from PyQt5.QtPrintSupport import QPrinter, QPrintDialog,QPrintPreviewDialog
from PyQt5 import QtCore,QtWidgets,QtGui
import sys

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        self.setGeometry(100, 100, 800, 600)
        self.setWindowTitle('QTextDocument Example')

        central_widget = QWidget()
        layout = QVBoxLayout()

        self.text_edit = QTextEdit()
        layout.addWidget(self.text_edit)

        self.generate_button = QPushButton('Generate Document')
        self.generate_button.clicked.connect(self.generateDocument)
        layout.addWidget(self.generate_button)

        central_widget.setLayout(layout)
        self.setCentralWidget(central_widget)

    def generateDocument(self):
        # Clear any previous content
        self.text_edit.clear()

        # Create a QTextDocument instance
        doc = QTextDocument()
        cursor = QTextCursor(doc)
        content = "HI there"
        cursor.insertText(content)
        printer = QPrinter()
        printer.setFullPage(True)
        printer.setPageMargins(0.1,0.1,0.1,0.1,QPrinter.Millimeter)
        printer.setPaperSize(QPrinter.PageSize.Letter)
        printer.setOrientation(QPrinter.Orientation.Portrait)
        doc.setDocumentMargin(0)
        self.text_edit.setDocument(doc)
        doc.print_(printer)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainWindow = MainWindow()
    mainWindow.show()
    sys.exit(app.exec_())

Here is the picture of one of the outputs The Content have a lot of margin on the left side.

enter image description here

Can anyone help please??

0

There are 0 best solutions below