1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ##############################################################################
22 """ Copyright (c) 2003-2007 LOGILAB S.A. (Paris, FRANCE).
23 http://www.logilab.fr/ -- mailto:contact@logilab.fr
25 manipulate pdf and fdf files. pdftk recommended.
27 Notes regarding pdftk, pdf forms and fdf files (form definition file)
28 fields names can be extracted with:
29 pdftk orig.pdf generate_fdf output truc.fdf
31 pdftk orig.pdf fill_form test.fdf output result.pdf [flatten]
32 without flatten, one could further edit the resulting form.
33 with flatten, everything is turned into text.
60 def output_field( f ):
61 return "\xfe\xff" + "".join( [ "\x00"+c for c in f ] )
63 def extract_keys(lines):
66 if line.startswith('/V'):
68 elif line.startswith('/T'):
70 key = ''.join(key.split('\x00'))
74 def write_field(out, key, value):
77 out.write("/V (%s)\n" %value)
80 out.write("/T (%s)\n" % output_field(key) )
83 def write_fields(out, fields):
87 write_field(out, key, value)
90 def extract_keys_from_pdf(filename):
91 # what about using 'pdftk filename dump_data_fields' and parsing the output ?
92 os.system('pdftk %s generate_fdf output /tmp/toto.fdf' % filename)
93 lines = file('/tmp/toto.fdf').readlines()
94 return extract_keys(lines)
97 def fill_pdf(infile, outfile, fields):
98 write_fields(file('/tmp/toto.fdf', 'w'), fields)
99 os.system('pdftk %s fill_form /tmp/toto.fdf output %s flatten' % (infile, outfile))
101 def testfill_pdf(infile, outfile):
102 keys = extract_keys_from_pdf(infile)
105 fields.append( (key, key, '') )
106 fill_pdf(infile, outfile, fields)
109 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: