Skip to content

Instantly share code, notes, and snippets.

@cgspeck
Last active November 8, 2021 08:56
Show Gist options
  • Select an option

  • Save cgspeck/27a8f2bce1bab764cefd158fe14e2670 to your computer and use it in GitHub Desktop.

Select an option

Save cgspeck/27a8f2bce1bab764cefd158fe14e2670 to your computer and use it in GitHub Desktop.
De-watermark a PDF
#! /bin/bash -e
# inspired by https://superuser.com/questions/448519/how-to-remove-watermark-from-pdf-using-pdftk
if [ $# -ne 2 ]; then
echo -e "Usage:\n ${0} \"watermark text\" input.pdf\n"
exit 1
fi
INPUT_FILENAME=$2
WM_STRING=$1
if [ ! -r $2 ]; then
echo "Could not read ${2}"
exit 1
fi
filename="${INPUT_FILENAME%.*}"
output_filename="${filename}-out.pdf"
tmp_uncompressed=$(mktemp)
trap "rm -f $tmp_uncompressed" 0 2 3 15
tmp_unwatermarked=$(mktemp)
trap "rm -f $tmp_unwatermarked" 0 2 3 15
echo "Uncompressing..."
pdftk "${INPUT_FILENAME}" output "${tmp_uncompressed}" uncompress
echo "Removing text..."
sed -e "s/${WM_STRING}//" "${tmp_uncompressed}" > "${tmp_unwatermarked}"
echo "Compressing and writing to ${output_filename}"
pdftk "${tmp_unwatermarked}" output "${output_filename}" compress
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment