clean up temporary files after stamping
This commit is contained in:
parent
d94f172b32
commit
539ee2d4d4
2 changed files with 17 additions and 6 deletions
21
stamps.py
21
stamps.py
|
@ -35,10 +35,11 @@ class MissingStampError(Exception):
|
|||
Not necessarily an actual error, depending on the calling situation.
|
||||
|
||||
If there is information about a stamp but some information,
|
||||
e.g. the posX, then a ConfigError should be raised and no
|
||||
e.g. the posX, is missing, then a ConfigError should be raised and no
|
||||
MissingStampError.
|
||||
|
||||
No further implementation since Exception includes everything necessary.
|
||||
|
||||
"""
|
||||
|
||||
|
||||
|
@ -281,7 +282,8 @@ def stampPdfWithStamps(stamps, pdf, newpdf=None):
|
|||
Attributes:
|
||||
stamps ([Stamp]): stamps to be added.
|
||||
pdf (str): path to the pdf that gets the text added.
|
||||
newpdf (str): path to the newly created pdf (default: =pdf-stamped.pdf)
|
||||
newpdf (str): path to the newly created pdf (if None, use
|
||||
pdf-stamped.pdf)
|
||||
|
||||
Raises:
|
||||
various IOError
|
||||
|
@ -289,18 +291,27 @@ def stampPdfWithStamps(stamps, pdf, newpdf=None):
|
|||
"""
|
||||
if newpdf is None or newpdf == pdf:
|
||||
# Default
|
||||
newpdf = os.path.splitext(pdf)[0] + "-stamped.pdf"
|
||||
tmpnewpdf = os.path.splitext(pdf)[0] + "-stamped.pdf"
|
||||
else:
|
||||
tmpnewpdf = newpdf
|
||||
|
||||
totalpagenumber = getNumberOfPagespdf(pdf)
|
||||
stamppdffile = os.path.join(TMPDIR, os.path.basename(newpdf))
|
||||
stamppdffile = os.path.join(TMPDIR, os.path.basename(tmpnewpdf))
|
||||
createStamppdf(stamppdffile, stamps, totalpagenumber)
|
||||
stamp = subprocess.Popen(["pdftk", pdf, "multistamp", stamppdffile,
|
||||
"output", newpdf], stdout=subprocess.PIPE,
|
||||
"output", tmpnewpdf], stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE)
|
||||
_, error = stamp.communicate()
|
||||
if b"Error" in error:
|
||||
raise FileNotFoundError("Adding a stamp to file " + pdf + " failed:"
|
||||
+ "\n" + str(error))
|
||||
else:
|
||||
# we do not need original:
|
||||
os.rename(tmpnewpdf, newpdf)
|
||||
for f in os.listdir(TMPDIR):
|
||||
os.remove(os.path.join(TMPDIR, f))
|
||||
os.rmdir(TMPDIR)
|
||||
# todo: how can I just remove a directory like rm -r?
|
||||
|
||||
|
||||
def stampPdf(formfields, pdf, newpdf=None):
|
||||
|
|
|
@ -64,4 +64,4 @@ def fillpdf(fields, pdf, pdfout, fdf):
|
|||
|
||||
"""
|
||||
fillpdfform(fields, pdf, pdfout, fdf)
|
||||
stamps.stampPdf(fields, pdfout)
|
||||
stamps.stampPdf(fields, pdfout, pdfout)
|
||||
|
|
Loading…
Reference in a new issue