forked from krispouille/reportlab-wrapper
/
wrapper.py
269 lines (208 loc) · 8.73 KB
/
wrapper.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
from reportlab.pdfgen import canvas
from reportlab.platypus import Frame, Paragraph, Image, ParagraphAndImage, Preformatted, Table, TableStyle
from reportlab.lib.utils import ImageReader, simpleSplit
from reportlab.pdfbase.pdfmetrics import stringWidth
from reportlab.lib.units import cm,inch
from reportlab.lib.styles import getSampleStyleSheet,ParagraphStyle
from reportlab.lib import pagesizes, colors
from reportlab.lib.colors import HexColor
landscape = pagesizes.landscape
portrait = pagesizes.portrait
A4 = pagesizes.A4
class Document(canvas.Canvas):
def __init__(self, filename, title=None, pagesize=None):
''' extends/initialize canvas '''
title = '<Unknown>' if title==None else title
size = A4 if pagesize==None else pagesize
canvas.Canvas.__init__(self, filename, pagesize=size)
self.setTitle(title)
self.pageCount = 0
self.pagesize = size
self.width, self.height = self.pagesize
self.x = 0
self.y = 0
self.left = 0
self.right = 0
self.top = 0
self.bottom = 0
def placement(self, width=0, height=0, left=0, bottom=0, top=None, right=None):
'''
returns (x,y) position for an element depending on :
- width = int | '0-100%'
- height = int | '0-100%'
- left = int | 'center' | '0-100%'
- bottom = int | 'center' | '0-100%'
- top = int | 'center' | '0-100%'
- right = int | 'center' | '0-100%'
'''
x = y = 0
width = checkPercent(width, self.width)
left = checkPercent(left, self.width)
right = checkPercent(right, self.width)
height = checkPercent(height, self.height)
top = checkPercent(top, self.height)
bottom = checkPercent(bottom, self.height)
if left=='center' or right=='center':
x = self.width/2-width/2
else:
x = left
x = x if right==None else self.width-width-right
if top=='center' or bottom=='center':
y = self.height/2-height/2
else:
y = bottom
y = y if top==None else self.height-height-top
return (x,y)
def padding(self,bottom=None,top=None,left=None,right=None):
''' edit paddings '''
bottom = bottom if bottom!=None else self.bottom
top = top if top!=None else self.top
left = left if left!=None else self.left
right = right if right!=None else self.right
tx = left-self.left
ty = bottom-self.bottom
x = tx if tx!=0 else left
y = ty if ty!=0 else bottom
self.bottom = bottom
self.top = top
self.left = left
self.right = right
self.width = self.pagesize[0]-self.left-self.right
self.height = self.pagesize[1]-self.bottom-self.top
#print self.width,self.height,tx, ty
self.translate(tx,ty)
def frame(self,story,width='100%',height='100%',left=0,bottom=0,top=None,right=None):
''' create a zone that will contain flowable elements '''
self.saveState()
width = checkPercent(width, self.width)
height = checkPercent(height, self.height)
x,y = self.placement(width,height,left,bottom,top,right)
f = Frame(x,y,width,height)
f.addFromList(story,self)
self.restoreState()
def page(self,*args,**config):
'''
Configure a new page in the PDF
Merge the defaults page configuration with the new one
Apply all changes to this new page
'''
def _page(c):
''' sub-function to handle display of a page'''
if self.pageCount > 0 :
self.showPage()
self.translate(self.left,self.bottom)
self.setPageSize(self.pagesize)
color = c.pop('color') if 'color' in c else None
layout = c.pop('layout') if 'layout' in c else None
self.padding(**c)
self.pageCount = self.pageCount + 1
if color: self.color(color)
if layout: layout(self)
story = list(args)
if 'pagesize' in config:
self.pagesize = config.pop('pagesize')
self.width,self.height = self.pagesize
if len(story)==0:
_page(config)
else:
while(story):
_page(dict(config))
self.frame(story)
def _image(self,width=None, height=None):
w,h = width,height
def call(filename):
return Image(filename, width=w, height=h)
return call
def image(self, filename, width=None, height=None, left=0, bottom=0, top=None, right=None, mask=None, preserveAspectRatio=False, anchor='c'):
''' add an image to canvas '''
self.saveState()
image = ImageReader(filename)
width = image.getSize()[0] if width==None else width
height = image.getSize()[1] if height==None else height
width = checkPercent(width, self.width)
height = checkPercent(height, self.height)
x, y = self.placement(width, height, left, bottom, top, right)
self.drawImage(image, x, y, width, height, mask, preserveAspectRatio, anchor)
self.restoreState()
def _table(self, vAlign='top', align='left', paddingTop=0, paddingBottom=0, paddingLeft=0, paddingRight=0, colWidths=None):
''' returns a function that will create a configured table '''
cfg = [
#('BACKGROUND', (0,0),(0,-1), HexColor('#eeeeee')),
#('BACKGROUND', (-1,0),(-1,-1), HexColor('#ffcc00')),
]
if vAlign!=None:
cfg.append(['VALIGN',(0,0),(-1,-1),vAlign.upper()])
if align!=None:
cfg.append(['ALIGN',(0,0),(-1,-1),align.upper()])
if paddingTop!=None:
cfg.append(['TOPPADDING',(0,0),(-1,-1),paddingTop])
if paddingBottom!=None:
cfg.append(['BOTTOMPADDING',(0,0),(-1,-1),paddingBottom])
if paddingLeft!=None:
cfg.append(['LEFTPADDING',(0,0),(-1,-1),paddingLeft])
if paddingRight!=None:
cfg.append(['RIGHTPADDING',(0,0),(-1,-1),paddingRight])
def call(*datas):
return Table(list(datas), colWidths=colWidths, style=TableStyle(cfg))
return call
def _text(self,font='Times-Roman',size=13,letterSpacing=None,color='#000000',marginTop=0,marginBottom=6,preformatted=False,style=None):
''' returns a function that will create a formatted text '''
letterSpacing=size*1.2 if letterSpacing==None else letterSpacing
def call(text):
pstyle = ParagraphStyle('p',fontName=font,fontSize=size,leading=letterSpacing,textColor=color,spaceBefore=marginTop,spaceAfter=marginBottom)
text = '<b>'+text+'</b>' if style!=None and style.lower()=='bold' else text
text = '<i>'+text+'</i>' if style!=None and style.lower()=='italic' else text
if preformatted==True:
return Preformatted(text,pstyle)
else:
return Paragraph(text,pstyle)
return call
def text(self, text, width=None, height=None, left=0, bottom=0, top=None, right=None, style=None):
''' add a text/paragraph to canvas '''
self.saveState()
style = getSampleStyleSheet()['Normal'] if style==None else style
p = Paragraph(text,style)
width = checkPercent(width, self.width)
height = checkPercent(height, self.height)
width = stringWidth(p.getPlainText(),style.fontName, style.fontSize)+1 if width==None else width
height = style.leading*len(simpleSplit(p.getPlainText(), style.fontName, style.fontSize, width)) if height==None else height
width = self.width if width > self.width else width
width, height = p.wrap(width, height)
x,y = self.placement(width, height, left, bottom, top, right)
p.drawOn(self,x,y)
self.restoreState()
def rectangle(self,width='100%',height='100%',left=0,bottom=0,top=None,right=None,color='#eeeeee'):
''' draw a rectangle '''
self.saveState()
self.setFillColor(HexColor(color))
width = checkPercent(width, self.width)
height = checkPercent(height, self.height)
x,y = self.placement(width,height,left,bottom,top,right)
self.rect(x,y,width,height,stroke=0,fill=1)
self.restoreState()
def color(self, c):
''' set a background color to a page '''
self.rectangle(color=c,width='100%',height='100%')
def line(self,width='100%',height=1,left=None,bottom=None,top=None,right=None,color='#000'):
''' draw a line '''
self.rectangle(width,height,left,bottom,top,right,color)
def close(self):
''' close current page and save the document '''
self.showPage()
self.save()
def checkPercent(string, reference):
'''
for a given percentage in string (formatted as 'xx%') and an integer as reference,
it returns the integer value. Examples :
print checkPercent('10%',100) returns 10 (good format "xx%")
print checkPercent(1200,100) returns 1200 (bad format)
'''
if string==None: return string
try:
return int(string)
except:
value = string[:-1]
if string[-1]=='%' and value.isdigit() and int(value) in range(101):
return reference*int(value)/100
else:
return string