Kat*_*tya 6 html qt4 pyqt4 python-3.x
我有以下certificate类用于从一些图像和数据中生成pdf文档.设置图像源后,我调用generate()函数并获取test.pdf输出文件.该文档是基于QTextDocument类使用setHtml(html)方法创建的.
问题是我在文档周围有很大的空白区域,而我希望带有徽标图像的标题"REPORT"位于页面的最顶层.我还想在表格中添加下边框,但据我所知,Qt(支持的HTML子集)不支持它.
Python3代码:
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)
Run Code Online (Sandbox Code Playgroud)
我之前从未广泛使用过html,也从未使用过QTextDocument,并且非常感谢有关如何控制文档边距和表格属性的任何建议.
我想控制的其他相关属性是分辨率 - 我使用像素图像大小,需要知道页面和边距大小(以像素为单位).
编辑:问题几乎由@mata回答.我现在可以设置任何边距和分辨率,但不了解如何控制图像和字体大小.例如,如果我需要一个图像总是50毫米宽,并且html标题和主文本字体大小在视觉上是相同的 - 如何实现它?
EDITED2:最后一部分也解决了.这是@mata的修改代码,它为任何dpi值提供相同的结果:
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)
...
Run Code Online (Sandbox Code Playgroud)
您可以在创建QPrinter时指定要在构造函数中使用的分辨率.然后在你设置了pagesize之后,你可以使用width,height并resolution在打印机上找出这些值,这是我得到的字母(dpi值可能不同,它们取决于屏幕或打印机):
QPrinter(QPrinter.ScreenResolution) # 96dpi, 752x992
QPrinter(QPrinter.PrinterResolution) # 72dpi, 564x744
QPrinter(QPrinter.HighResolution) # 1200dpi, 9400x12400
Run Code Online (Sandbox Code Playgroud)
您也可以直接使用dpi进行设置setResolution.宽度和高度返回的大小是页面大小(与pageRect().size()相同),它与纸张大小不同 - 因为页面也有边距,您可以像这样设置:
printer.setPageMargins(12, 16, 12, 20, QPrinter.Millimeter)
Run Code Online (Sandbox Code Playgroud)
这样可以将左右边距设置为12mm,顶部设置为16mm,底部设置为20mm - 例如,如果您想要更少的空白区域,您显然可以使用更小的值.您应该将文档大小设置为结果大小的大小:
document.setPageSize(QSizeF(printer.pageRect().size()))
Run Code Online (Sandbox Code Playgroud)
正如您已经注意到的那样,html和css允许的子集非常有限,特别是对于格式化表格.但是,不要在桌面上使用下边框,而只需使用hr,这可能看起来像你想要的那样.如果我像这样测试它,它至少看起来不那么糟糕:
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)
Run Code Online (Sandbox Code Playgroud)