Skip to content

Commit

Permalink
Merge pull request #5 from yguarata/master
Browse files Browse the repository at this point in the history
Adding a function to replace a PDF page by other PDF.
  • Loading branch information
Julien Bouquillon committed Jan 26, 2016
2 parents 737dd8b + 733e58d commit 1fd078c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ Generate a XFDF file suited for filling PDF forms and return the generated XFDF
Return the number of pages for a given PDF
- `pdf_path` : input PDF file


### `replace_page`
Replace a page in a PDF (pdf_path) by the PDF pointed by pdf_to_insert_path.
- pdf_path is the PDF that will have its page replaced.
- page_number is the number of the page in pdf_path to be replaced. It is 1-based.
- pdf_to_insert_path is the PDF that will be inserted at the old page.


## Example
Expand Down
14 changes: 14 additions & 0 deletions pypdftk.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,17 @@ def gen_xfdf(datas={}):
f.close()
return out_file

def replace_page(pdf_path, page_number, pdf_to_insert_path):
"""
Replace a page in a PDF (pdf_path) by the PDF pointed by pdf_to_insert_path.
page_number is the number of the page in pdf_path to be replaced. It is 1-based.
"""
A = 'A=' + pdf_path
B = 'B=' + pdf_to_insert_path
lower_bound = 'A1-' + str(page_number - 1)
upper_bound = 'A' + str(page_number + 1) + '-end'
output_temp = tempfile.mktemp(suffix='.pdf')
args = (PDFTK_PATH, A, B, 'cat', lower_bound, 'B', upper_bound, 'output', output_temp)
run_command(args)
shutil.copy(output_temp, pdf_path)
os.remove(output_temp)

0 comments on commit 1fd078c

Please sign in to comment.