Package logilab-common-0 :: Package 36 :: Package 1 :: Module pdf_ext
[frames] | no frames]

Source Code for Module logilab-common-0.36.1.pdf_ext

 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  # XXX seems very unix specific 
21  # TODO: check availability of pdftk at import  
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   
47 -def output_field( f ):
48 return "\xfe\xff" + "".join( [ "\x00"+c for c in f ] )
49
50 -def extract_keys(lines):
51 keys = [] 52 for line in lines: 53 if line.startswith('/V'): 54 pass #print 'value',line 55 elif line.startswith('/T'): 56 key = line[7:-2] 57 key = ''.join(key.split('\x00')) 58 keys.append( key ) 59 return keys
60
61 -def write_field(out, key, value):
62 out.write("<<\n") 63 if value: 64 out.write("/V (%s)\n" %value) 65 else: 66 out.write("/V /\n") 67 out.write("/T (%s)\n" % output_field(key) ) 68 out.write(">> \n")
69
70 -def write_fields(out, fields):
71 out.write(HEAD) 72 for (key,value,comment) in fields: 73 write_field(out, key, value) 74 write_field(out, key+"a", value) # pour copie-carbone sur autres pages 75 out.write(TAIL)
76
77 -def extract_keys_from_pdf(filename):
78 # what about using 'pdftk filename dump_data_fields' and parsing the output ? 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
84 -def fill_pdf(infile, outfile, fields):
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
88 -def testfill_pdf(infile, outfile):
89 keys = extract_keys_from_pdf(infile) 90 fields = [] 91 for key in keys: 92 fields.append( (key, key, '') ) 93 fill_pdf(infile, outfile, fields)
94