Created
August 4, 2017 17:48
-
-
Save elfsternberg/a96883018d783cbbad7b454ecd0a7ffe to your computer and use it in GitHub Desktop.
Remove a textual watermark from a PDF file.
This file contains 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 | |
# Remove a textual watermark from a PDF file. Requires qpdf and pdftx | |
# to work correctly. The correct usage is | |
# | |
# remove-watermark WATERMARK "Your Input File.pdf" "Your Output File.pdf" | |
WATERMARK=$1 | |
INBOUND=$2 | |
OUTBOUND=$3 | |
UNCOMPRESSED=`mktemp --dry-run 'uncompressed-XXXXXXXXXX.pdf'` | |
FIXED=`mktemp --dry-run 'fixed-XXXXXXXXXX.pdf'` | |
UNMARKED=`mktemp --dry-run 'unmarked-XXXXXXXXXX.pdf'` | |
WATERMARKLEN=${#WATERMARK} | |
BLANKS=`printf %${WATERMARKLEN}s` | |
qpdf --stream-data=uncompress "$INBOUND" $UNCOMPRESSED | |
sed -e 's/\[($WATERMARK)\]/[($BLANKS)]/g' < $UNCOMPRESSED > $FIXED | |
pdftk $FIXED output $UNMARKED | |
qpdf --stream-data=compress $UNMARKED "$OUTBOUND" | |
rm $UNCOMPRESSED $FIXED $UNMARKED | |
# NO WARRANTY | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Thanks for this. Wrote a variant since I was getting sed: RE error: illegal byte sequence
and pdftk
doesn't work on newer versions of OS X: https://gist.github.com/convexset/28abc9c7be261954507ac705cbf64099
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Elf,
thanks for your great tool. Can I also remove a multiline watermark?
E.g. at https://www.peterlang.com/downloadpdf/title/17680.pdf
Kind regards