PDA

View Full Version : Remove the Margins of a QtextEdit or QtextDocument when printing.



Saelyth
14th September 2017, 17:32
Hi,
I have a QTextEdit filled with just HTML and I want to print it (either to any print or as PDF). However, the output file always shows way too much margins or borders and I want to remove them.

https://image.prntscr.com/image/0mpZ5jsDSkSpsdCPrGTY7w.png

This is my PyQt5 Code:


setupdialog = QPrintDialog()
if setupdialog.exec() == QPrintDialog.Accepted:
printer = setupdialog.printer()
textReport.print(printer)
textReport is a QtextEdit() created with Qt Creator for a .ui file (I can control how the html looks like inside the QtextEdit that way and I can guarantee it show no margins at all in Qt Creator Design mode).

I've also tried all of this options separately:

printer.setPageMargins(0.1, 0.1, 0.1, 0.1, QPrinter.Millimeter) #left top right bottom
printer.setFullPage(True)
printer.setPageMargins(QPrinter.Millimeter)
printer.setPageRect(QPrinter.Millimeter)
printer.setPaperRect(QPrinter.Millimeter)
printer.setPageSizeMM()
printer.setResolution()
printer.setPaperName()
textReport.setContentsMargins(0, 0, 0, 0)
QTextDocument.setIndentWidth()
I also tried to use a QtextDocument instead. No luck so far. Any insight?

Note: This attempt is the result of a research trying to print a Widget to PDF as you can check here: https://stackoverflow.com/questions/46134250/pdf-output-not-working-with-pyqt5-and-python-3-5

Saelyth
15th September 2017, 02:14
Small update with something weird to me:


print("Margins: " + str(self.textReport.getContentsMargins()))
self.textReport.setContentsMargins(2, 2, 2, 2)
print("New margins: " + str(self.textReport.getContentsMargins()))
Outputs:

Margins: (1, 1, 1, 1)
New margins: (1, 1, 1, 1)

Shouldn't it be the 2nd output 2, 2, 2, 2?
Is this a bug of Qt5 or PyQt5?