python-3.x - 如何正确设置打印机的painter?
问题描述
我正在打印一组表格,每个表格都应该有自己的页面并且可能很长。基础工作正常,但我没有画出页脚。问题是页脚将被绘制在额外的文档中。
根据文档,我必须将画家设置为设备。该设备是画家,这是正确的,但我如何将画家设置为正确的块?还是这样做是不对的?
目标是使用此文档两次。第一次尝试是打印,第二次是QTextDocument
我可以在其中找到QTextTable
's 并与另一个文档元素一起编译它。
工作示例
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtPrintSupport import *
content = [['section 1', [1,2,3,4]],['section2', [5,6,7,8]]]
app = QApplication(sys.argv)
document = QTextDocument ()
printer = QPrinter()
painter = QPainter(printer)
pageRect = printer.pageRect ()
tableFormat = QTextTableFormat ()
cellBlockFormat = QTextBlockFormat ()
cellCharFormat = QTextCharFormat ()
cellCharFormat.setFont (QFont ("Arial", 10))
for rownr, line in enumerate(content):
cursor = QTextCursor (document)
mainFrame = cursor.currentFrame ()
# header
cursor.setPosition (mainFrame.firstPosition ())
cursor.insertHtml ("This is the table for %s"%line[0])
# table
table = cursor.insertTable (3, 4, tableFormat)
for colnr, col in enumerate(line[1]):
print("col:", col)
cellCursor = table.cellAt (rownr + 1, colnr).firstCursorPosition ()
cellCursor.setBlockFormat (cellBlockFormat)
cellCursor.insertText (str (col))
#footer
painter.begin(printer)
painter.drawText (0, pageRect.bottom(), "I may be the footer")
painter.end()
# section finished
cursor.setPosition (mainFrame.lastPosition ())
tableFormat.setPageBreakPolicy (QTextFormat.PageBreak_AlwaysAfter)
cursor.insertBlock (cellBlockFormat, cellCharFormat)
document.print_(printer)
解决方案
前提:这与其说是一种解决方案,不如说是一种技巧,因为它是一种肮脏的解决方法。
这个想法是继承 QPrinter,覆盖该newPage
方法并相应地绘制页脚。这需要printer
使用页脚手动更新实例。
不幸的是,还有另一个重要问题:只要只有一页,我就无法打印页脚。
在接下来的几天里,我会尝试再次调查它,看看是否有解决方案。
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtPrintSupport import *
class FooterPrinter(QPrinter):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.footers = {}
def paintEngine(self, *args):
engine = super().paintEngine(*args)
self.currentPage = 0
return engine
def drawFooter(self):
text = self.footers.get(self.currentPage)
if not text:
return
painter = super().paintEngine().painter()
dpiy = painter.device().logicalDpiY()
margin = dpiy * (2/2.54)
rect = QRectF(margin, margin, self.width() - margin * 2, self.height() - margin * 2)
fm = QFontMetrics(painter.font(), painter.device())
size = fm.size(0, text)
painter.drawText(rect.left(), rect.bottom() - size.height(),
size.width(), size.height(), Qt.AlignLeft|Qt.AlignTop, text)
def newPage(self):
self.drawFooter()
newPage = super().newPage()
if newPage:
self.currentPage += 1
self.drawFooter()
return newPage
content = [['section 1', [1,2,3,4]],['section2', [5,6,7,8]]]
app = QApplication(sys.argv)
document = QTextDocument()
printer = FooterPrinter()
printer.setOutputFileName('/tmp/test.pdf')
pageRect = printer.pageRect ()
tableFormat = QTextTableFormat ()
cellBlockFormat = QTextBlockFormat ()
cellCharFormat = QTextCharFormat ()
cellCharFormat.setFont (QFont ("Arial", 10))
for rownr, line in enumerate(content):
cursor = QTextCursor (document)
mainFrame = cursor.currentFrame ()
# header
cursor.setPosition (mainFrame.firstPosition ())
cursor.insertHtml ("This is the table for %s"%line[0])
# table
table = cursor.insertTable (3, 4, tableFormat)
for colnr, col in enumerate(line[1]):
cellCursor = table.cellAt (rownr + 1, colnr).firstCursorPosition ()
cellCursor.setBlockFormat (cellBlockFormat)
cellCursor.insertText (str (col))
cursor.setPosition (mainFrame.lastPosition ())
tableFormat.setPageBreakPolicy (QTextFormat.PageBreak_AlwaysAfter)
cursor.insertBlock (cellBlockFormat, cellCharFormat)
printer.footers[rownr] = 'Note for page {}: some text.\nNew line\nAnother new line'.format(rownr + 1)
document.print_(printer)
推荐阅读
- ios - CLLocation2D 没有从变量中获得赋值
- c# - 如何找出正在运行的 Discord 机器人实例的数量?
- spring-boot - spring boot + cassandra 轻量级事务更新
- kotlin - println() 产生不同的输出
- r - 删除相同的列,同时从每个组中保留一个
- python - 我应该如何拆分 LSF 字符串?
- fortran - OpenMP 4.0 标准是否支持现代 Fortran 中的类型绑定过程?
- android - 何时将应用程序数据保存在内存中?
- sql - 从 2 个表中填充 Listview
- javascript - javascript中的构造函数和API接口有何不同?