initial commit
[identyfikatory.git] / generate.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4
5 # use mms
6 page_width = 210
7 page_height = 297
8 margin_left = 5
9 margin_top = 28.5
10 extra_pages = 1
11 with_lines = True
12
13
14 from copy import deepcopy
15 from lxml import etree
16 import re
17 import os
18
19 page_template = etree.parse('wzor.svg')
20 orig_text_height = float(os.popen('inkscape --query-id=textroot -H wzor.svg').read())
21
22 def mm_to_px(mm):
23     return float(mm) / 25.4 * 90
24
25 def get_translate(elem):
26         match = re.match("translate\((-?\d*\.?\d*),(-?\d*\.?\d*)\)", elem.get('transform'))
27         return float(match.group(1)), float(match.group(2))
28
29 def set_translate(elem, x, y):
30         elem.set('transform', "translate(%f,%f)" % (x, y))
31
32
33 root = page_template.getroot()
34 template_width = float(root.get('width'))
35 template_height = float(root.get('height'))
36
37 grid_width = int(mm_to_px(page_width) / template_width)
38 grid_height = int(mm_to_px(page_height) / template_height)
39 on_page = grid_height * grid_width
40
41 nazwiska = []
42 import sys
43 for fname in sys.argv[1:]:
44         with open(fname) as f:
45                 nazwiska += [(n.decode('utf-8').strip('\n') + '\t\t\t').split('\t')[:4] for n in f]
46
47 nazwiska.extend(
48         ['', '', '', ''] for i in range((len(nazwiska) / on_page + (extra_pages + 1)) * on_page - len(nazwiska)))
49
50 root.set('width', str(mm_to_px(page_width)))
51 root.set('height', str(mm_to_px(page_height)))
52 template = root.find('{http://www.w3.org/2000/svg}g')
53 transform_x, transform_y = get_translate(template)
54 root.remove(template)
55
56 line = etree.Element('{http://www.w3.org/2000/svg}path')
57 line.set('style', 'stroke: #888; stroke-width: .1mm;')
58
59 x = y = page = 0
60 output_odd = deepcopy(page_template)
61 output_even = deepcopy(page_template)
62 for nazwisko, imie, org, funkcja in nazwiska:
63         print "%s (%s)" % (nazwisko, org)
64         card = deepcopy(template)
65         flow = card.find('.//{http://www.w3.org/2000/svg}flowRoot')
66         current_id = 'card-%d-%d' % (x, y)
67         flow.set('id', current_id)
68         for elem in flow.findall('.//{http://www.w3.org/2000/svg}flowPara'):
69                 if elem.text == u'ImiÄ™ i nazwisko':
70                         elem.text = imie + ' ' + nazwisko
71                 elif elem.text == u'Organizacja':
72                         elem.text = org
73                 elif elem.text == u'Funkcja':
74                         elem.text = funkcja
75         set_translate(card,
76                 transform_x + mm_to_px(margin_left) + x * template_width,
77                 transform_y + mm_to_px(margin_top) + y * template_height
78         )
79         output_even.getroot().append(card)
80         # Check height
81         output_even.write('tmp.svg')
82         text_height = float(os.popen('inkscape --query-id=%s -H tmp.svg' % (current_id,)).read())
83         tx, ty = get_translate(flow)
84         set_translate(flow, tx, ty + (orig_text_height - text_height) / 2)
85
86         odd_card = deepcopy(card)
87         odd_card.set('transform', "translate(%f,%f)" % (
88                 transform_x + mm_to_px(page_width) - mm_to_px(margin_left) - (x + 1) * template_width,
89                 transform_y + mm_to_px(margin_top) + y * template_height
90         ))
91         output_odd.getroot().append(odd_card)
92
93         x += 1
94         if x >= grid_width:
95                 y += 1
96                 x = 0
97         if y >= grid_height:
98                 if with_lines:
99                         for xx in range(grid_width + 1):
100                                 l = deepcopy(line)
101                                 l.set('d', 'm %f,%f %f,%f' % (
102                                                 mm_to_px(margin_left) + xx * template_width,
103                                                 0,
104                                                 0,
105                                                 mm_to_px(margin_top - 1),
106                                         ))
107                                 output_even.getroot().append(l)
108                                 l = deepcopy(line)
109                                 l.set('d', 'm %f,%f %f,%f' % (
110                                                 mm_to_px(margin_left) + xx * template_width,
111                                                 mm_to_px(margin_top + 1) + grid_height * template_height,
112                                                 0,
113                                                 mm_to_px(page_height) - mm_to_px(margin_top + 1) - grid_height * template_height,
114                                         ))
115                                 output_even.getroot().append(l)
116                         for yy in range(grid_height + 1):
117                                 l = deepcopy(line)
118                                 l.set('d', 'm %f,%f %f,%f' % (
119                                                 0,
120                                                 mm_to_px(margin_top) + yy * template_height,
121                                                 mm_to_px(margin_left - 1),
122                                                 0,
123                                         ))
124                                 output_even.getroot().append(l)
125                                 l = deepcopy(line)
126                                 l.set('d', 'm %f,%f %f,%f' % (
127                                                 mm_to_px(margin_left + 1) + grid_width * template_width,
128                                                 mm_to_px(margin_top) + yy * template_height,
129                                                 mm_to_px(page_width) - mm_to_px(margin_left + 1) - grid_width * template_width,
130                                                 0,
131                                         ))
132                                 output_even.getroot().append(l)
133
134                 output_even.write('out-%02d.svg' % (2 * page))
135                 output_odd.write('out-%02d.svg' % (2 * page + 1))
136                 output_odd = deepcopy(page_template)
137                 output_even = deepcopy(page_template)
138                 page += 1
139                 x = y = 0
140
141 if x or y:
142         output_even.write('out-%02d.svg' % (2 * page))
143         output_odd.write('out-%02d.svg' % (2 * page + 1))
144         page += 1
145
146 for p in range(2 * page):
147         os.system('inkscape out-%02d.svg -A out-%02d.pdf' % (p, p))
148
149 os.system('pdftk %s cat output output.pdf' % (" ".join("out-%02d.pdf" % p for p in range(2*page))))