Launchpad automatic translations update.
[odoo/odoo.git] / bin / tools / pdf_utils.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #    
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 #
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.
11 #
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.
16 #
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/>.     
19 #
20 ##############################################################################
21
22 """ Copyright (c) 2003-2007 LOGILAB S.A. (Paris, FRANCE).
23  http://www.logilab.fr/ -- mailto:contact@logilab.fr
24
25 manipulate pdf and fdf files. pdftk recommended.
26
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
30 to merge fdf and pdf:
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.
34 """
35
36 from __future__ import with_statement
37
38 import os
39 import tempfile
40
41
42 HEAD="""%FDF-1.2
43 %\xE2\xE3\xCF\xD3
44 1 0 obj
45 <<
46 /FDF
47 <<
48 /Fields [
49 """
50
51 TAIL="""]
52 >>
53 >>
54 endobj
55 trailer
56
57 <<
58 /Root 1 0 R
59 >>
60 %%EOF
61 """
62
63 def output_field(f):
64     return "\xfe\xff" + "".join( [ "\x00"+c for c in f ] )
65
66 def extract_keys(lines):
67     keys = []
68     for line in lines:
69         if line.startswith('/V'):
70             pass #print 'value',line
71         elif line.startswith('/T'):
72             key = line[7:-2]
73             key = ''.join(key.split('\x00'))
74             keys.append( key )
75     return keys
76
77 def write_field(out, key, value):
78     out.write("<<\n")
79     if value:
80         out.write("/V (%s)\n" %value)
81     else:
82         out.write("/V /\n")
83     out.write("/T (%s)\n" % output_field(key) )
84     out.write(">> \n")
85
86 def write_fields(out, fields):
87     out.write(HEAD)
88     for key in fields:
89         value = fields[key]
90         write_field(out, key, value)
91 #        write_field(out, key+"a", value) # pour copie-carbone sur autres pages
92     out.write(TAIL)
93
94 def extract_keys_from_pdf(filename):
95     # what about using 'pdftk filename dump_data_fields' and parsing the output ?
96     tmp_file = tempfile.mkstemp(".fdf")[1]
97     try:
98         os.system('pdftk %s generate_fdf output \"%s\"' % (filename, tmp_file))
99         with open(tmp_file, "r") as ofile:
100             lines = ofile.readlines()
101     finally:
102         try:
103             os.remove(tmp_file)
104         except Exception:
105             pass # nothing to do
106     return extract_keys(lines)
107
108
109 def fill_pdf(infile, outfile, fields):
110     tmp_file = tempfile.mkstemp(".fdf")[1]
111     try:
112         with open(tmp_file, "w") as ofile:
113             write_fields(ofile, fields)
114         os.system('pdftk %s fill_form \"%s\" output %s flatten' % (infile, tmp_file, outfile))
115     finally:
116         try:
117             os.remove(tmp_file)
118         except Exception:
119             pass # nothing to do
120
121 def testfill_pdf(infile, outfile):
122     keys = extract_keys_from_pdf(infile)
123     fields = []
124     for key in keys:
125         fields.append( (key, key, '') )
126     fill_pdf(infile, outfile, fields)
127
128
129 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
130