1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
#!/usr/bin/python
# -*- coding: ISO-8859-1 -*-
import sys
from PySide import *
class Report(QtGui.QFrame):
''' '''
def __init__(self):
super(Report, self).__init__()
# Définition des marges
margeGauche = 30
margeHaut = 100
margeDroite = 10
margeBas = 40
margeHeader = 20
margeFooter = 40
printer = QtGui.QPrinter()
printer.setPageMargins(margeGauche, margeHaut, margeDroite, margeBas, QtGui.QPrinter.DevicePixel)
innerRect = printer.pageRect()
sizeF = QtCore.QSizeF(innerRect.size().width(), innerRect.size().height())
# Définition des header, footer et document à imprimer
doc = QtGui.QTextDocument()
self.doc = doc
cursor = QtGui.QTextCursor(doc)
format1 = QtGui.QTextCharFormat()
format1.setFontPointSize(16)
format2 = QtGui.QTextCharFormat()
format2.setFontPointSize(8)
cursor.insertText(" 999 Titre de l'état -- Suite __ suite vvgg == abcde 111 " * 10, format1)
cursor.insertText("Titre de l'état -- Suite __ suite vvgg == abcde " * 400, format2)
header = QtGui.QTextDocument()
header.setPageSize(sizeF)
self.header = header
cursor_header = QtGui.QTextCursor(header)
format1 = QtGui.QTextCharFormat()
format1.setFontPointSize(16)
cursor_header.insertText("------EnTete --- EnTete --------" * 20)
cursor_header.select(QtGui.QTextCursor.Document)
fragment_header = cursor_header.selection()
footer = QtGui.QTextDocument()
footer.setPageSize(sizeF)
self.footer = footer
cursor_footer = QtGui.QTextCursor(footer)
cursor_footer.insertText("------Pied de page --- Pied de page --------" * 10)
cursor_footer.select(QtGui.QTextCursor.Document)
fragment_footer = cursor_footer.selection()
blockFormatSaut = QtGui.QTextBlockFormat()
blockFormatSaut.setPageBreakPolicy(QtGui.QTextFormat.PageBreak_AlwaysBefore)
blockFormatHeader = QtGui.QTextBlockFormat()
blockFormatHeader.setPageBreakPolicy(QtGui.QTextFormat.PageBreak_Auto)
blockFormatHeader.setTopMargin(margeHeader)
blockFormatFooter = QtGui.QTextBlockFormat()
blockFormatFooter.setPageBreakPolicy(QtGui.QTextFormat.PageBreak_Auto)
blockFormatFooter.setTopMargin(margeFooter)
doc.setPageSize(sizeF)
contentHeight = (innerRect.size().height() -
header.size().toSize().height() - margeHeader -
footer.size().toSize().height() - margeFooter)
contentSize = QtCore.QSize(innerRect.size().width(), contentHeight)
contentRect = QtCore.QRect(QtCore.QPoint(margeGauche, margeHaut), contentSize)
totalPages = doc.size().toSize().height() / contentHeight + 1
docF = QtGui.QTextDocument()
docF.setPageSize(contentSize)
cursorF = QtGui.QTextCursor(docF)
cursor.movePosition(QtGui.QTextCursor.Start, QtGui.QTextCursor.MoveAnchor)
docP = QtGui.QTextDocument()
docP.setPageSize(contentSize)
docF.setDocumentMargin(40)
cursorP = QtGui.QTextCursor(docP)
nb = 1
max = (contentHeight - margeHaut - margeBas - margeFooter
- margeHeader - header.size().toSize().height() - footer.size().toSize().height())
while docP.size().height() < max :
cursor.movePosition(QtGui.QTextCursor.Start, QtGui.QTextCursor.MoveAnchor)
cursor.movePosition(cursor.Down, QtGui.QTextCursor.KeepAnchor, nb)
docP.setHtml(cursor.selection().toHtml())
nb += 1
cursor.movePosition(QtGui.QTextCursor.Start, QtGui.QTextCursor.MoveAnchor)
cursor.movePosition(cursor.Down, QtGui.QTextCursor.KeepAnchor, nb - 2)
fragment = cursor.selection()
cursorF.movePosition(QtGui.QTextCursor.Start, QtGui.QTextCursor.MoveAnchor)
cursorF.insertFragment(fragment_header)
cursorF.insertBlock(blockFormatHeader)
cursorF.insertFragment(fragment)
cursorF.insertBlock(blockFormatFooter)
cursorF.insertFragment(fragment_footer)
dialog = QtGui.QPrintPreviewDialog()
dialog.paintRequested.connect(docF.print_)
dialog.exec_()
def main():
app = QtGui.QApplication(sys.argv)
frame = Report()
frame.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main() |
Partager