I have the following certificate
class for producing pdf document out of some images and data. After setting image sources, I call generate()
function and get test.pdf output file. The document is created based on QTextDocument
class using setHtml(html)
method.
The problem is that I have huge white spaces around the document, while I want the title 'REPORT' with logo image to be on the very top of the page. I would also like to add lower border to the table, but as I understand it is not supported by Qt (Supported HTML Subset).
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 never extensively used html before and never worked with QTextDocument, and would appreciate any advice on how to control document margins and table properties.
Other related property I want to control is resolution - I use pixel image size and need to know page and margin sizes in pixels.
EDITED: The question is almost answered by @mata. I can set now any margins and resolution, but do not understand how to control image and font sizes. E.g. if I need that an image is always 50mm wide, and html header and main text font sizes are visually the same - how to implement it?
EDITED2: The last part is solved too. Here is modified code by @mata, 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)
...