Skip to content

Commit

Permalink
Merge pull request #6 from yguarata/master
Browse files Browse the repository at this point in the history
Adding function to apply stamps in PDF.
  • Loading branch information
Julien Bouquillon committed Feb 4, 2016
2 parents 1fd078c + e330aa1 commit ae432e1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ Return the number of pages for a given PDF

### `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.
- `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.

### `stamp`
Applies a stamp (from `stamp_pdf_path`) to the PDF file in `pdf_path`. If no `output_pdf_path` is provided, it returns a temporary file with the result PDF.


## Example
Expand Down
16 changes: 13 additions & 3 deletions pypdftk.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ def gen_xfdf(datas={}):
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)
Expand All @@ -143,4 +143,14 @@ def replace_page(pdf_path, page_number, pdf_to_insert_path):
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)
os.remove(output_temp)

def stamp(pdf_path, stamp_pdf_path, output_pdf_path=None):
'''
Applies a stamp (from stamp_pdf_path) to the PDF file in pdf_path. Useful for watermark purposes.
If not output_pdf_path is provided, it returns a temporary file with the result PDF.
'''
output = output_pdf_path or tempfile.mktemp(suffix='.pdf')
args = [PDFTK_PATH, pdf_path, 'multistamp', stamp_pdf_path, 'output', output]
run_command(args)
return output

0 comments on commit ae432e1

Please sign in to comment.