问题描述
我的发票
我在 python 中有一个几乎完成的项目,它使用 qprintpreviwdialog 根据上图显示和打印数据.我使用 qtextdocumento 来处理 html.
有没有办法在页码空间中写一些东西.我想将黄色信息写入页码空间作为页脚.或者是否有其他百家乐凯发k8的解决方案来显示页脚而不是使用不属于 python 的 pyjasper?
将
my invoice
i have a almost finishd project in python which uses qprintpreviwdialog to show and print data according to the picture above. i have used qtextdocumento to handle the html.
is there a way to write something in page number space. i would like to write the info in yellow to the page number space to serve as footer. or is there other solution to show footer instead of using pyjasper, one that are not part of python?
translating my previous answer from c to python and modifying the position of the text i show how to add a footer.
from pyqt5 import qtcore, qtgui, qtwidgets, qtprintsupport textmargins = 12 bordermargins = 10 def mmtopixels(printer, mm): return mm * 0.039370147 * printer.resolution() def paintpage(pagenumber, pagecount, painter, doc, textrect, footerheight): painter.save() textpagerect = qtcore.qrectf(qtcore.qpointf(0, pagenumber*doc.pagesize().height()), doc.pagesize()) painter.setcliprect(textrect) painter.translate(0, -textpagerect.top()) painter.translate(textrect.left(), textrect.top()) doc.drawcontents(painter) painter.restore() footerrect = qtcore.qrectf(textrect) footerrect.settop(textrect.bottom()) footerrect.setheight(footerheight) # draw footer painter.save() pen = painter.pen() pen.setcolor(qtcore.qt.blue) painter.setpen(pen) painter.drawtext(footerrect, qtcore.qt.aligncenter, "page {} of {}".format(pagenumber 1, pagecount)) painter.restore() def printdocument(printer, doc): painter = qtgui.qpainter(printer) doc.documentlayout().setpaintdevice(printer) doc.setpagesize(qtcore.qsizef(printer.pagerect().size())) pagesize = printer.pagerect().size() tm = mmtopixels(printer, textmargins) footerheight = painter.fontmetrics().height() textrect = qtcore.qrectf(tm, tm, pagesize.width() - 2 * tm, pagesize.height() - 2 * tm - footerheight) doc.setpagesize(textrect.size()) pagecount = doc.pagecount() for pageindex in range(pagecount): if pageindex != 0: printer.newpage() paintpage(pageindex, pagecount, painter, doc, textrect, footerheight) if __name__ == '__main__': import sys app = qtwidgets.qapplication(sys.argv) document = qtgui.qtextdocument() cursor = qtgui.qtextcursor(document) blockformat = qtgui.qtextblockformat() for i in range(10): cursor.insertblock(blockformat) cursor.inserthtml("为 qprintwidget、qprintprevwiwwidget 添加页脚-百家乐凯发k8
".format(i 1)) blockformat.setpagebreakpolicy(qtgui.qtextformat.pagebreak_alwaysbefore) printer = qtprintsupport.qprinter(qtprintsupport.qprinter.highresolution) printer.setpagesize(qtprintsupport.qprinter.a4) printer.setoutputformat(qtprintsupport.qprinter.pdfformat) dialog = qtprintsupport.qprintpreviewdialog(printer) dialog.paintrequested.connect(lambda print, doc=document: printdocument(printer, doc)) dialog.exec_()