Project

General

Profile

Download (7.13 KB) Statistics
| Branch: | Tag: | Revision:

calebasse / calebasse / dossiers / transport_template.py @ 54318d2c

1
# -*- coding: utf-8 -*-
2

    
3
import os
4
import tempfile
5

    
6
import cairo
7
import pango
8
import pangocairo
9

    
10
from ..pdftk import PdfTk
11

    
12

    
13
class TransportTemplate(object):
14
    fields = {
15
            'NOM_BENEFICIAIRE': {
16
                'pos': (302, 186),
17
            },
18
            'PRENOM_BENEFICIAIRE': {
19
                'pos': (136, 200),
20
            },
21
            'DATE': {
22
                'pos': (532, 597),
23
                'size': 8,
24
            },
25
            'LIEU': {
26
                'pos': (596, 597),
27
            },
28
            'IDENTIFICATION_ETABLISSEMENT': {
29
                'type': 'multiline',
30
                'pos': (510, 652),
31
            },
32
            'NIR_ASSURE': {
33
                'pos': (325, 800),
34
                'size': 10,
35
            },
36
            'NIR_KEY_ASSURE': {
37
                'pos': (527, 800),
38
                'size': 10,
39
            },
40
            'NOM_ASSURE': {
41
                'pos': (492, 825),
42
            },
43
            'CODE_ORGANISME_1': {
44
                'pos': (328, 850),
45
                'size': 10,
46
            },
47
            'CODE_ORGANISME_2': {
48
                'pos': (357, 850),
49
                'size': 10,
50
            },
51
            'CODE_ORGANISME_3': {
52
                'pos': (401, 850),
53
                'size': 10,
54
            },
55
            'ADRESSE_BENEFICIAIRE': {
56
                'pos': (92, 891),
57
            },
58
            'SITUATION_CHOICE_1': {
59
                'pos': (393, 264),
60
                'type': 'bool',
61
            },
62
            'SITUATION_CHOICE_2': {
63
                'pos': (393, 280),
64
                'type': 'bool',
65
            },
66
            'SITUATION_CHOICE_3': {
67
                'pos': (719, 264),
68
                'type': 'bool',
69
            },
70
            'SITUATION_CHOICE_4': {
71
                'pos': (719, 280),
72
                'type': 'bool',
73
            },
74
            'SITUATION_DATE': {
75
                'pos': (610, 299),
76
                'size': 8,
77
            },
78
            'TRAJET_TEXT': {
79
                'pos': (84, 340),
80
                'size': 8,
81
            },
82
            'TRAJET_CHOICE_1': {
83
                'pos': (201, 396),
84
                'type': 'bool',
85
            },
86
            'TRAJET_CHOICE_2': {
87
                'pos': (454, 396),
88
                'type': 'bool',
89
            },
90
            'TRAJET_CHOICE_3': {
91
                'pos': (719, 396),
92
                'type': 'bool',
93
            },
94
            'TRAJET_CHOICE_4': {
95
                'pos': (201, 419),
96
                'type': 'bool',
97
            },
98
            'TRAJET_NUMBER': {
99
                'pos': (305, 409),
100
                'size': 8,
101
            },
102
            'PC_CHOICE_1': {
103
                'pos': (567, 435),
104
                'type': 'bool',
105
            },
106
            'PC_CHOICE_2': {
107
                'pos': (642, 435),
108
                'type': 'bool',
109
            },
110
            'MODE_CHOICE_1': {
111
                'pos': (321, 484),
112
                'type': 'bool',
113
            },
114
            'MODE_CHOICE_2': {
115
                'pos': (321, 499),
116
                'type': 'bool',
117
            },
118
            'MODE_CHOICE_3': {
119
                'pos': (321, 515),
120
                'type': 'bool',
121
            },
122
            'MODE_CHOICE_4': {
123
                'pos': (568, 528),
124
                'type': 'bool',
125
            },
126
            'MODE_CHOICE_5': {
127
                'pos': (642, 528),
128
                'type': 'bool',
129
            },
130
            'MODE_CHOICE_6': {
131
                'pos': (321, 546),
132
                'type': 'bool',
133
            },
134
            'CDTS_CHOICE_1': {
135
                'pos': (404, 565),
136
                'type': 'bool',
137
            },
138
            'CDTS_CHOICE_2': {
139
                'pos': (567, 563),
140
                'type': 'bool',
141
            },
142
    }
143

    
144
    def __init__(self, template_path=None, prefix='tmp', suffix=''):
145
        self.prefix = prefix
146
        self.suffix = suffix
147
        self.template_path = template_path
148

    
149
    def draw_field(self, ctx, field, value, size=9):
150
        _type = field.get('type', 'text')
151
        size = field.get('size', size)
152
        if _type == 'bool':
153
            x, y = field['pos']
154
            if value:
155
                ctx.move_to(x, y - 10)
156
                pangocairo_context = pangocairo.CairoContext(ctx)
157
                pangocairo_context.set_antialias(cairo.ANTIALIAS_SUBPIXEL)
158

    
159
                layout = pangocairo_context.create_layout()
160
                font = pango.FontDescription("Georgia %s" % size)
161
                layout.set_font_description(font)
162

    
163
                layout.set_text(u'\u2714')
164
                pangocairo_context.update_layout(layout)
165
                pangocairo_context.show_layout(layout)
166
        if _type in ('text', 'multiline'):
167
            x, y = field['pos']
168
            ctx.move_to(x, y)
169
            pangocairo_context = pangocairo.CairoContext(ctx)
170
            pangocairo_context.set_antialias(cairo.ANTIALIAS_SUBPIXEL)
171

    
172
            layout = pangocairo_context.create_layout()
173
            font = pango.FontDescription("Georgia Bold %s" % size)
174
            layout.set_font_description(font)
175

    
176
            layout.set_text(unicode(value))
177
            pangocairo_context.update_layout(layout)
178
            if field.get('border'):
179
                a, b, width, height = layout.get_pixel_extents()[1]
180
                ctx.save()
181
                ctx.set_source_rgb(1, 1, 1)
182
                ctx.rectangle(x - 2, y - 2, width + 4, height + 4)
183
                ctx.fill()
184
                ctx.set_source_rgb(0, 0, 0)
185
                ctx.rectangle(x - 2, y - 2, width + 4, height + 4)
186
                ctx.set_line_width(0.1)
187
                ctx.stroke()
188
                ctx.restore()
189
            ctx.move_to(x, y)
190
            pangocairo_context.show_layout(layout)
191
        if _type == 'array':
192
            field = field.copy()
193
            y = field['y']
194
            offset = field.get('x', 0)
195
            for row in value:
196
                for x, v in zip(field['cols'], row):
197
                    sub = {
198
                            'pos': (offset + x, y),
199
                            'size': size,
200
                    }
201
                    self.draw_field(ctx, sub, v, 7)
202
                y += field['lineheight']
203

    
204
    def generate(self, content, delete=True):
205
        width, height = 827, 1169
206
        with tempfile.NamedTemporaryFile(prefix=self.prefix,
207
                suffix=self.suffix, delete=False) as temp_out_pdf:
208
            try:
209
                overlay = tempfile.NamedTemporaryFile(prefix='overlay',
210
                        suffix='.pdf')
211
                surface = cairo.PDFSurface(overlay.name, width, height)
212
                ctx = cairo.Context(surface)
213
                ctx.set_source_rgb(0, 0, 0)
214
                for key, value in content.iteritems():
215
                    field = self.fields[key]
216
                    self.draw_field(ctx, field, value)
217
                ctx.show_page()
218
                surface.finish()
219
                pdftk = PdfTk()
220
                pdftk.background(overlay.name, self.template_path,
221
                    temp_out_pdf.name)
222
                overlay.close()
223
                return temp_out_pdf.name
224
            except:
225
                if delete:
226
                    try:
227
                        os.unlink(temp_out_pdf.name)
228
                    except:
229
                        pass
230
                raise
(9-9/12)