diff options
| author | Numerigraphe - Lionel Sausin <ls@numerigraphe.fr> | 2010-11-12 15:10:41 +0100 |
|---|---|---|
| committer | Numerigraphe - Lionel Sausin <ls@numerigraphe.fr> | 2010-11-12 15:10:41 +0100 |
| commit | 182cf2b196da0d1f55ddfdcff62058b423e1bbc0 (patch) | |
| tree | 779b50e2e54b1624ba669b4a3f56bb6407c08d49 /bin/tools/pdf_utils.py | |
| parent | e013734045bbaae906748a8026455bac6786568e (diff) | |
| parent | cc0d270bab0d0b7862430c3e527f7edc02d1d587 (diff) | |
[MERGE] sync with the trunk
bzr revid: ls@numerigraphe.fr-20101112141041-19kpvr7pzxqankqo
Diffstat (limited to 'bin/tools/pdf_utils.py')
| -rw-r--r-- | bin/tools/pdf_utils.py | 28 |
1 files changed, 23 insertions, 5 deletions
diff --git a/bin/tools/pdf_utils.py b/bin/tools/pdf_utils.py index 0286b9cf46d..7d9bf3af700 100644 --- a/bin/tools/pdf_utils.py +++ b/bin/tools/pdf_utils.py @@ -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 @@ -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) |
