- 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