From 4aab8e55fcf43ddad1a15ddb33165faf518038a9 Mon Sep 17 00:00:00 2001 From: user Date: Thu, 4 Feb 2016 12:45:51 -0200 Subject: [PATCH 1/3] Adding function to apply stamps in PDF. --- pypdftk.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/pypdftk.py b/pypdftk.py index 30d53d2..30c5adb 100644 --- a/pypdftk.py +++ b/pypdftk.py @@ -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) @@ -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) \ No newline at end of file + 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 \ No newline at end of file From 5ea7be4faa9c6336171adfe07a500979dd42b416 Mon Sep 17 00:00:00 2001 From: user Date: Thu, 4 Feb 2016 12:58:22 -0200 Subject: [PATCH 2/3] Updating the readme. --- README.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f1f9e98..2c4e9c8 100644 --- a/README.md +++ b/README.md @@ -36,9 +36,13 @@ 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`. Useful for watermark purposes. +If not `output_pdf_path` is provided, it returns a temporary file with the result PDF. ## Example From e330aa152efd2396c4deaf0f2b53d56a4c72a045 Mon Sep 17 00:00:00 2001 From: user Date: Thu, 4 Feb 2016 13:18:05 -0200 Subject: [PATCH 3/3] Updating readme. --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 2c4e9c8..f2d627a 100644 --- a/README.md +++ b/README.md @@ -41,8 +41,7 @@ Replace a page in a PDF (pdf_path) by the PDF pointed by pdf_to_insert_path. - `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`. Useful for watermark purposes. -If not `output_pdf_path` is provided, it returns a temporary file with the result PDF. +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