1 """Manipulate pdf and fdf files (pdftk recommended).
2
3 Notes regarding pdftk, pdf forms and fdf files (form definition file)
4 fields names can be extracted with:
5
6 pdftk orig.pdf generate_fdf output truc.fdf
7
8 to merge fdf and pdf:
9
10 pdftk orig.pdf fill_form test.fdf output result.pdf [flatten]
11
12 without flatten, one could further edit the resulting form.
13 with flatten, everything is turned into text.
14
15 :copyright: 2000-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
16 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
17 :license: General Public License version 2 - http://www.gnu.org/licenses
18 """
19 __docformat__ = "restructuredtext en"
20
21
22
23
24 import os
25
26 HEAD="""%FDF-1.2
27 %\xE2\xE3\xCF\xD3
28 1 0 obj
29 <<
30 /FDF
31 <<
32 /Fields [
33 """
34
35 TAIL="""]
36 >>
37 >>
38 endobj
39 trailer
40
41 <<
42 /Root 1 0 R
43 >>
44 %%EOF
45 """
46
48 return "\xfe\xff" + "".join( [ "\x00"+c for c in f ] )
49
51 keys = []
52 for line in lines:
53 if line.startswith('/V'):
54 pass
55 elif line.startswith('/T'):
56 key = line[7:-2]
57 key = ''.join(key.split('\x00'))
58 keys.append( key )
59 return keys
60
69
76
78
79 os.system('pdftk %s generate_fdf output /tmp/toto.fdf' % filename)
80 lines = file('/tmp/toto.fdf').readlines()
81 return extract_keys(lines)
82
83
85 write_fields(file('/tmp/toto.fdf', 'w'), fields)
86 os.system('pdftk %s fill_form /tmp/toto.fdf output %s flatten' % (infile, outfile))
87
94