Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stamp only the last page of a PDF

Tags:

pdf

pdftk

I have the following code to stamp a pdf with a an image that is converted to a pdf. Everything seems to work except for it stamps every page in the pdf. How can i get it to only stamp the last page of the pdf?

pdftk temp-pdfjam.pdf stamp signature-pdfjam.pdf output final/finalized.pdf
like image 456
Brad Hazelnut Avatar asked Dec 13 '13 18:12

Brad Hazelnut


2 Answers

If you want to work only with pdftk, it looks like you will have to extract the last page using the cat command of pdftk, stamp it, then join it with the first pages - or prepare a PDF with the appropriate number of blank pages and use the "multistamp" command.

like image 61
Jan Schejbal Avatar answered Sep 23 '22 03:09

Jan Schejbal


A working snippet using pdftk

#!/bin/sh

input=$1
watermark=$2
output=$3
abl=/tmp/all_but_last.pdf
last=/tmp/last.pdf
last_stamped=/tmp/last_stamped.pdf

# detach
pdftk $1 cat 1-r2 output $abl 
pdftk $1 cat end output $last 
#Stamp
pdftk $last stamp $watermark output $last_stamped
#Attach
pdftk $abl $last_stamped cat output $output

Run it as a shell script :

./stamp_last_page.sh input.pdf stamp.pdf output.pdf

like image 24
thechargedneutron Avatar answered Sep 21 '22 03:09

thechargedneutron