How to set QTextDocument fields and other properties (setHTML, print in pdf)?

I have the following certificate class to create a pdf document from some images and data. After setting the image sources, I call the generate() function and get the output file test.pdf. The document is created based on the QTextDocument class using the setHtml(html) method.

The problem is that I have huge white spaces around the document, while I want the โ€œREPORTโ€ heading with the logo to be at the very top of the page. I would also like to add a bottom border to the table, but as I understand it, it is not supported by Qt ( Supported subset of HTML ).

Python3 Code:

 class certificate: def __init__(self): self.logo = None pdffile = 'test.pdf' self.histogram = None self.printer = QPrinter() self.printer.setPageSize(QPrinter.Letter) self.printer.setOutputFormat(QPrinter.PdfFormat) self.printer.setOutputFileName(pdffile) def generate(self): document = QTextDocument() html = "" html += ('<head><title>Report</title><style></style></head>' '<body><table width="100%"><tr>' '<td><img src="{}" width="30"></td>' '<td><h1>REPORT</h1></td>' '</tr></table>' '<p align=right><img src="{}" width="300"></p>' '<p align=right>Sample</p></body>').format(self.logo, self.histogram) document.setHtml(html) document.print_(self.printer) 

I have never used html before and have never worked with QTextDocument, and would appreciate any advice on managing fields and table properties.

Another related property that I want to control is resolution - I use the size of the pixel image and must know the size of the pages and margins in pixels.

EDITED: @mata almost answered the question. I can set any fields and resolution, but I donโ€™t understand how to manage image and font sizes. For instance. if I need the image to always have a width of 50 mm, and the dimensions of the html header and the main text are visually the same - how to implement it?

EDITED2: The last part is also resolved. The @mata code is changed here, it gives the same result for any dpi value:

 dpi=96 document = QTextDocument() html = """ <head> <title>Report</title> <style> </style> </head> <body> <table width="100%"> <tr> <td><img src="{0}" width="{1}"></td> <td><h1>REPORT</h1></td> </tr> </table> <hr> <p align=right><img src="{2}" width="{3}"></p> <p align=right>Sample</p> </body> """.format('D:\Documents\IST Projects\diashape\docbook\Installation\images\istlogo_medium.png', 40*dpi/96, 'D:\Documents\IST Projects\diashape\docbook\Installation\images\istlogo_medium.png', 200*dpi/96) document.setHtml(html) printer = QPrinter() font = QFont() font.setPointSize(12*dpi/96) document.setDefaultFont(font) printer.setResolution(dpi) ... 
+6
source share
1 answer

You can specify which permission you want to use in the constructor when creating QPrinter. Then, after you set the page size, you can use the width , height and resolution on the printer to crop these values, this is what I got for the letters (the dpi values โ€‹โ€‹may be different, they depend on the screen or printer):

 QPrinter(QPrinter.ScreenResolution) # 96dpi, 752x992 QPrinter(QPrinter.PrinterResolution) # 72dpi, 564x744 QPrinter(QPrinter.HighResolution) # 1200dpi, 9400x12400 

You can also configure dpi with setResolution . The size returned in width and height is the page size (the same as pageRect (). Size ()), which does not match the paper size - since the page also has margins that you can set as follows:

 printer.setPageMargins(12, 16, 12, 20, QPrinter.Millimeter) 

this sets the left and right margins to 12 mm, from above to 16 mm and from below to 20 mm - for example, if you want less free space, you can simply use smaller values. And you should set the size of the document to the size of the resulting size:

 document.setPageSize(QSizeF(printer.pageRect().size())) 

as you noticed yourself, a subset of html and css is allowed very limited, especially for formatting tables. But instead of using the bottom border on the table, you can just use hr, which will probably look the way you want. At least it doesn't look so bad if I test it like this:

 from PyQt4.QtGui import * from PyQt4.QtCore import * a=QApplication([]) document = QTextDocument() html = """ <head> <title>Report</title> <style> </style> </head> <body> <table width="100%"> <tr> <td><img src="{}" width="30"></td> <td><h1>REPORT</h1></td> </tr> </table> <hr> <p align=right><img src="{}" width="300"></p> <p align=right>Sample</p> </body> """.format('', '') document.setHtml(html) printer = QPrinter() printer.setResolution(96) printer.setPageSize(QPrinter.Letter) printer.setOutputFormat(QPrinter.PdfFormat) printer.setOutputFileName("test.pdf") printer.setPageMargins(12, 16, 12, 20, QPrinter.Millimeter) document.setPageSize(QSizeF(printer.pageRect().size())) print(document.pageSize(), printer.resolution(), printer.pageRect()) document.print_(printer) 
+6
source

Source: https://habr.com/ru/post/914016/


All Articles