root / logilab.pylintinstaller / logilab / common / pdf_ext.py

Revision 202:d67e86292521, 2.8 kB (checked in by tziade@…, 9 months ago)

added logilab.pylintinstaller

Line 
1# This program is free software; you can redistribute it and/or modify
2# it under the terms of the GNU General Public License as published by
3# the Free Software Foundation; either version 2 of the License, or
4# (at your option) any later version.
5#
6# This program is distributed in the hope that it will be useful, but WITHOUT
7# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details
9#
10# You should have received a copy of the GNU General Public License along with
11# this program; if not, write to the Free Software Foundation, Inc.,
12# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
13""" Copyright (c) 2003-2007 LOGILAB S.A. (Paris, FRANCE).
14 http://www.logilab.fr/ -- mailto:contact@logilab.fr
15
16manipulate pdf and fdf files. pdftk recommended.
17
18Notes regarding pdftk, pdf forms and fdf files (form definition file)
19fields names can be extracted with:
20    pdftk orig.pdf generate_fdf output truc.fdf
21to merge fdf and pdf:     
22    pdftk orig.pdf fill_form test.fdf output result.pdf [flatten]
23without flatten, one could further edit the resulting form.
24with flatten, everything is turned into text.
25"""
26
27import os
28
29HEAD="""%FDF-1.2
30%\xE2\xE3\xCF\xD3
311 0 obj
32<<
33/FDF
34<<
35/Fields [
36"""
37
38TAIL="""]
39>>
40>>
41endobj
42trailer
43
44<<
45/Root 1 0 R
46>>
47%%EOF
48"""
49
50def output_field( f ):
51    return "\xfe\xff" + "".join( [ "\x00"+c for c in f ] )
52
53def extract_keys(lines):
54    keys = []
55    for line in lines:
56        if line.startswith('/V'):
57            pass #print 'value',line
58        elif line.startswith('/T'):
59            key = line[7:-2]
60            key = ''.join(key.split('\x00'))
61            keys.append( key )
62    return keys
63
64def write_field(out, key, value):
65    out.write("<<\n")
66    if value:
67        out.write("/V (%s)\n" %value)
68    else:
69        out.write("/V /\n")
70    out.write("/T (%s)\n" % output_field(key) )
71    out.write(">> \n")
72
73def write_fields(out, fields):
74    out.write(HEAD)
75    for (key,value,comment) in fields:
76        write_field(out, key, value)
77        write_field(out, key+"a", value) # pour copie-carbone sur autres pages
78    out.write(TAIL)
79
80def extract_keys_from_pdf(filename):
81    # what about using 'pdftk filename dump_data_fields' and parsing the output ?
82    os.system('pdftk %s generate_fdf output /tmp/toto.fdf' % filename)
83    lines = file('/tmp/toto.fdf').readlines()
84    return extract_keys(lines)
85
86
87def fill_pdf(infile, outfile, fields):
88    write_fields(file('/tmp/toto.fdf', 'w'), fields)
89    os.system('pdftk %s fill_form /tmp/toto.fdf output %s flatten' % (infile, outfile))
90
91def testfill_pdf(infile, outfile):
92    keys = extract_keys_from_pdf(infile)
93    fields = []
94    for key in keys:
95        fields.append( (key, key, '') )
96    fill_pdf(infile, outfile, fields)
Note: See TracBrowser for help on using the browser.