
问题是我在文档周围有很大的空白区域,而我希望带有徽标图像的标题“REPORT”位于页面的最顶层.我还想在表格中添加下边框,但据我所知,Qt(Supported HTML Subset)不支持.
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) 我之前从未广泛使用过HTML,也从未使用过QTextdocument,并且对如何控制文档边距和表格属性表示赞赏.
我想控制的其他相关属性是分辨率 – 我使用像素图像大小,需要知道页面和边距大小(以像素为单位).
编辑:@mata几乎回答了这个问题.我现在可以设置任何边距和分辨率,但不了解如何控制图像和字体大小.例如.如果我需要一个图像总是50毫米宽,并且HTML标题和主文本字体大小在视觉上是相同的 – 如何实现它?
EDITED2:最后一部分也解决了.这是@mata的修改代码,它为任何dpi值提供相同的结果:
dpi=96document = 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)...解决方法 您可以在创建QPrinter时指定要在构造函数中使用的分辨率. 然后在设置了pagesize之后,你可以使用打印机上的宽度,高度和分辨率来确定这些值,这是我得到的字母(dpi值可以不同,它们取决于屏幕或打印机):
QPrinter(QPrinter.ScreenResolution) # 96dpi,752x992QPrinter(QPrinter.PrinterResolution) # 72dpi,564x744QPrinter(QPrinter.HighResolution) # 1200dpi,9400x12400
您也可以使用setResolution直接设置dpi.
宽度和高度返回的大小是页面大小(与pageRect(..size())相同,这与纸张大小不同 – 因为页面也有边距,您可以像这样设置:
printer.setPagemargins(12,16,12,20,QPrinter.Millimeter)
这将左右边距设置为12mm,顶部设置为16mm,底部设置为20mm – 例如,如果您想要更少的空白区域,您显然可以使用更小的值.
您应该将文档大小设置为结果大小的大小:
document.setPageSize(QSizef(printer.pageRect().size()))
正如您已经注意到的那样,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,QPrinter.Millimeter)document.setPageSize(QSizef(printer.pageRect().size()))print(document.pageSize(),printer.resolution(),printer.pageRect())document.print_(printer) 总结 以上是内存溢出为你收集整理的如何设置QTextDocument边距和其他属性(setHTML,print to pdf)?全部内容,希望文章能够帮你解决如何设置QTextDocument边距和其他属性(setHTML,print to pdf)?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)