Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding flatten only feature without filling forms #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions pypdftk.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,33 @@ def fill_form(pdf_path, datas={}, out_file=None, flatten=True, drop_xfa=False):
os.remove(tmp_fdf)
return out_file

def flatten_only(pdf_path, out_file=None, flatten=True, drop_xfa=True):
'''
Flattens a PDF.
Return temp file if no out_file provided.
'''
cleanOnFail = False
handle = None
if not out_file:
cleanOnFail = True
handle, out_file = tempfile.mkstemp()

cmd = "%s %s output %s" % (PDFTK_PATH, pdf_path, out_file)
if flatten:
cmd += ' flatten'
if drop_xfa:
cmd += ' drop_xfa'
try:
run_command(cmd, True)
except:
if cleanOnFail:
os.remove(out_file)
raise
finally:
if handle:
os.close(handle)
return out_file

def dump_data_fields(pdf_path, add_id=False):
'''
Return list of dicts of all fields in a PDF.
Expand Down