Launchpad automatic translations update.
[odoo/odoo.git] / bin / tools / pdf_utils.py
index 5e58f2f..7d9bf3a 100644 (file)
@@ -1,24 +1,24 @@
-# -*- encoding: utf-8 -*-
+# -*- coding: utf-8 -*-
 ##############################################################################
-#
+#    
 #    OpenERP, Open Source Management Solution
-#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
-#    $Id$
+#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
 #
 #    This program is free software: you can redistribute it and/or modify
-#    it under the terms of the GNU General Public License as published by
-#    the Free Software Foundation, either version 3 of the License, or
-#    (at your option) any later version.
+#    it under the terms of the GNU Affero General Public License as
+#    published by the Free Software Foundation, either version 3 of the
+#    License, or (at your option) any later version.
 #
 #    This program is distributed in the hope that it will be useful,
 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-#    GNU General Public License for more details.
+#    GNU Affero General Public License for more details.
 #
-#    You should have received a copy of the GNU General Public License
-#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#    You should have received a copy of the GNU Affero General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
 #
 ##############################################################################
+
 """ Copyright (c) 2003-2007 LOGILAB S.A. (Paris, FRANCE).
  http://www.logilab.fr/ -- mailto:contact@logilab.fr
 
@@ -33,8 +33,10 @@ without flatten, one could further edit the resulting form.
 with flatten, everything is turned into text.
 """
 
+from __future__ import with_statement
+
 import os
-import netsvc,logging
+import tempfile
 
 
 HEAD="""%FDF-1.2
@@ -58,7 +60,7 @@ trailer
 %%EOF
 """
 
-def output_field( f ):
+def output_field(f):
     return "\xfe\xff" + "".join( [ "\x00"+c for c in f ] )
 
 def extract_keys(lines):
@@ -91,14 +93,30 @@ def write_fields(out, fields):
 
 def extract_keys_from_pdf(filename):
     # what about using 'pdftk filename dump_data_fields' and parsing the output ?
-    os.system('pdftk %s generate_fdf output /tmp/toto.fdf' % filename)
-    lines = file('/tmp/toto.fdf').readlines()
+    tmp_file = tempfile.mkstemp(".fdf")[1]
+    try:
+        os.system('pdftk %s generate_fdf output \"%s\"' % (filename, tmp_file))
+        with open(tmp_file, "r") as ofile:
+            lines = ofile.readlines()
+    finally:
+        try:
+            os.remove(tmp_file)
+        except Exception:
+            pass # nothing to do
     return extract_keys(lines)
 
 
 def fill_pdf(infile, outfile, fields):
-    write_fields(file('/tmp/toto.fdf', 'w'), fields)
-    os.system('pdftk %s fill_form /tmp/toto.fdf output %s flatten' % (infile, outfile))
+    tmp_file = tempfile.mkstemp(".fdf")[1]
+    try:
+        with open(tmp_file, "w") as ofile:
+            write_fields(ofile, fields)
+        os.system('pdftk %s fill_form \"%s\" output %s flatten' % (infile, tmp_file, outfile))
+    finally:
+        try:
+            os.remove(tmp_file)
+        except Exception:
+            pass # nothing to do
 
 def testfill_pdf(infile, outfile):
     keys = extract_keys_from_pdf(infile)