Last active
November 8, 2021 08:56
-
-
Save cgspeck/27a8f2bce1bab764cefd158fe14e2670 to your computer and use it in GitHub Desktop.
De-watermark a PDF
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /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