Skip to content

Commit ae432e1

Browse files
author
Julien Bouquillon
committed
Merge pull request #6 from yguarata/master
Adding function to apply stamps in PDF.
2 parents 1fd078c + e330aa1 commit ae432e1

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ Return the number of pages for a given PDF
3636

3737
### `replace_page`
3838
Replace a page in a PDF (pdf_path) by the PDF pointed by pdf_to_insert_path.
39-
- pdf_path is the PDF that will have its page replaced.
40-
- page_number is the number of the page in pdf_path to be replaced. It is 1-based.
41-
- pdf_to_insert_path is the PDF that will be inserted at the old page.
39+
- `pdf_path` is the PDF that will have its page replaced.
40+
- `page_number` is the number of the page in pdf_path to be replaced. It is 1-based.
41+
- `pdf_to_insert_path` is the PDF that will be inserted at the old page.
42+
43+
### `stamp`
44+
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.
4245

4346

4447
## Example

pypdftk.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,10 @@ def gen_xfdf(datas={}):
131131
return out_file
132132

133133
def replace_page(pdf_path, page_number, pdf_to_insert_path):
134-
"""
134+
'''
135135
Replace a page in a PDF (pdf_path) by the PDF pointed by pdf_to_insert_path.
136136
page_number is the number of the page in pdf_path to be replaced. It is 1-based.
137-
"""
137+
'''
138138
A = 'A=' + pdf_path
139139
B = 'B=' + pdf_to_insert_path
140140
lower_bound = 'A1-' + str(page_number - 1)
@@ -143,4 +143,14 @@ def replace_page(pdf_path, page_number, pdf_to_insert_path):
143143
args = (PDFTK_PATH, A, B, 'cat', lower_bound, 'B', upper_bound, 'output', output_temp)
144144
run_command(args)
145145
shutil.copy(output_temp, pdf_path)
146-
os.remove(output_temp)
146+
os.remove(output_temp)
147+
148+
def stamp(pdf_path, stamp_pdf_path, output_pdf_path=None):
149+
'''
150+
Applies a stamp (from stamp_pdf_path) to the PDF file in pdf_path. Useful for watermark purposes.
151+
If not output_pdf_path is provided, it returns a temporary file with the result PDF.
152+
'''
153+
output = output_pdf_path or tempfile.mktemp(suffix='.pdf')
154+
args = [PDFTK_PATH, pdf_path, 'multistamp', stamp_pdf_path, 'output', output]
155+
run_command(args)
156+
return output

0 commit comments

Comments
 (0)