-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex8.py
169 lines (146 loc) · 5.59 KB
/
ex8.py
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import os, sys
project_dir = os.path.realpath(__file__).split("/examples")[0]
sys.path.append(project_dir)
from toet.models import Model
from toet.views import TableView
from toet.fields import TextField
from toet.fields import ImageField
from toet.templates import Template
from toet.settings import Setting
from reportlab.lib.enums import TA_CENTER, TA_LEFT, TA_RIGHT
from reportlab.platypus import Spacer
# In the previous example (ex7.pdf) we set
# cell alignment in header and body with
# self.span = False.
# Let's see here how to to do same but with
# self.span = True
class ExampleModel(Model):
def __init__(self, **kwargs):
super(ExampleModel, self).__init__(**kwargs)
self.one = TextField("One:<br/>{}".format(self._one))
self.two = TextField("Two:<br/>{}".format(self._two))
self.three = ImageField("images/Octocat.png", width=50, height=50)
self.four = TextField("Four:<br/>{}".format(self._four))
class ExampleView(TableView):
def __init__(self, **kwargs):
self.model = ExampleModel(**kwargs)
self.fields = [
['one', 'two'],
['one', 'two', 'three', 'four'],
['one'],
['one', 'two', 'four', 'three'],
['one', 'two', 'four'],
]
self.cell_alignment = {
"1":{
"1": [('VALIGN','BOTTOM')],
"3": [('VALIGN','TOP')]
},
"3":{
"1": [('VALIGN','TOP')],
"2": [('VALIGN','BOTTOM')]
},
"*": {
"1": [('VALIGN','BOTTOM')],
"2": [('VALIGN','TOP')],
}
}
self.style_header = {
"one": {
"style": "Normal",
"commands": [
('alignment', TA_CENTER),
('fontName', 'Helvetica'),
('fontSize', 8),
]
},
"two": {
"style": "Normal",
"commands": [
('alignment', TA_CENTER),
('fontName', 'Helvetica'),
('fontSize', 8),
]
},
"four": {
"style": "Normal",
"commands": [
('alignment', TA_CENTER),
('fontName', 'Helvetica'),
('fontSize', 8),
]
}
}
self.span = True
super(ExampleView, self).__init__(self)
class ExampleViewFirstRowDataBody(TableView):
def __init__(self, **kwargs):
self.model = ExampleModel(**kwargs)
self.fields = [
['one', 'two'],
['one', 'two', 'three', 'four'],
['one'],
['one', 'two', 'four'],
]
self.cell_alignment = {
"*": {
"1": [('VALIGN','BOTTOM')],
}
}
self.style_header = {
"one": {
"style": "Normal",
"commands": [
('alignment', TA_CENTER),
('fontName', 'Helvetica'),
('fontSize', 8),
]
},
"two": {
"style": "Normal",
"commands": [
('alignment', TA_CENTER),
('fontName', 'Helvetica'),
('fontSize', 8),
]
},
"four": {
"style": "Normal",
"commands": [
('alignment', TA_CENTER),
('fontName', 'Helvetica'),
('fontSize', 8),
]
}
}
self.span = True
self.body_header = self.fields[0]
super(ExampleViewFirstRowDataBody, self).__init__(self)
from reportlab.platypus import SimpleDocTemplate, Spacer
if __name__ == '__main__':
pdf = Template(filename='ex8.pdf')
data_model = {
"_one" : "1",
"_two" : "2",
"_four": "4",
}
data_body = [
['*<br/><br/><br/>', '*', '*'],
['*<br/><br/><br/>', '*', '*'],
['*<br/><br/><br/>', '*', '*'],
['*<br/><br/><br/>', '*', '*'],
]
table = ExampleView(**data_model)
rendered_table = table.render(data_body)
pdf.story.add(rendered_table)
some_space = Spacer(1, 10)
pdf.story.add(some_space)
first_row_data_body = [
['o<br/><br/>', 'o'],
['o<br/><br/>', 'o'],
['o<br/><br/>', 'o'],
]
table_first_row_data_body = ExampleViewFirstRowDataBody(**data_model)
rendered_table = table_first_row_data_body.render(first_row_data_body)
pdf.story.add(rendered_table)
pdf.build()