1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
12 from reportlab.platypus.doctemplate import SimpleDocTemplate
13 from reportlab.platypus import PageBreak
14 from vstf.controller.reporters.report.pdf.styles import TemplateStyle, ps_head_lv1, ps_head_lv2, ps_head_lv3
15 import vstf.common.constants as cst
16 from functools import reduce
19 class BaseDocTemplate(SimpleDocTemplate):
21 def __init__(self, filename, **kw):
22 self.allowSplitting = 0
23 SimpleDocTemplate.__init__(self, filename, **kw)
25 def afterFlowable(self, flowable):
26 """Registers TOC entries."""
27 if flowable.__class__.__name__ == 'Paragraph':
28 text = flowable.getPlainText()
29 style = flowable.style.name
30 if style == ps_head_lv1.name:
31 self.notify('TOCEntry', (0, text, self.page - 1))
32 elif style == ps_head_lv2.name:
33 self.notify('TOCEntry', (1, text, self.page - 1))
34 elif style == ps_head_lv3.name:
35 self.notify('TOCEntry', (2, text, self.page - 1))
38 class PdfTemplate(object):
40 def __init__(self, title, logo, header, footer, note=[], style="default"):
41 self._style = TemplateStyle(name=style)
44 #self._header = header[0]
47 info = " Generated on %s " % time.strftime(
48 cst.TIME_FORMAT2, time.localtime())
51 def myFirstPage(self, canvas, doc):
52 raise NotImplementedError("abstract StoryDecorator")
54 def myLaterPages(self, canvas, doc):
55 raise NotImplementedError("abstract StoryDecorator")
57 def generate(self, story, output):
58 sizes = (self._style.page_wight, self._style.page_height)
59 doc = BaseDocTemplate(output, pagesize=sizes)
60 # doc.build(story, onFirstPage=self.myFirstPage, onLaterPages=self.myLaterPages)
63 onFirstPage=self.myFirstPage,
64 onLaterPages=self.myLaterPages)
67 class PdfVswitch(PdfTemplate):
69 def myFirstPage(self, canvas, doc):
71 title_lines = len(self._title)
72 line_size = [self._style.title_size] * title_lines
77 (self._style.page_wight -
78 self._style.logo_width) /
80 self._style.page_height /
84 self._style.title_leading) *
90 self._style.logo_width,
91 self._style.logo_height)
92 for i in range(title_lines):
93 canvas.setFont(self._style.title_font, line_size[i])
94 canvas.drawCentredString(
95 self._style.page_wight /
97 self._style.page_height /
101 self._style.title_leading) *
110 size = self._style.body_size
111 canvas.setFont(self._style.body_font, size)
112 note_line = len(self._note)
114 for i in range(note_line):
116 canvas.drawCentredString(self._style.page_wight /
117 2.0, self._style.page_height /
120 self._style.body_leading) *
125 size = self._style.body_size - 2
126 canvas.setFont(self._style.body_font, size)
127 canvas.drawCentredString(self._style.page_wight /
128 2.0, self._style.page_bottom /
131 self._style.body_leading) *
132 size, self._footer[0])
133 canvas.restoreState()
135 def myLaterPages(self, canvas, doc):
137 canvas.setLineWidth(self._style.line_width)
138 canvas.line(self._style.page_left,
139 self._style.page_height - self._style.page_top,
140 self._style.page_wight - self._style.page_right,
141 self._style.page_height - self._style.page_top
143 size = self._style.body_size - 2
144 canvas.setFont(self._style.body_font, size)
145 canvas.drawCentredString(
146 self._style.page_wight / 2.0, self._style.page_bottom - 24, "%s%s Page %2d " %
147 (self._footer[0], " " * 8, doc.page - 1))
148 canvas.restoreState()