Skip to content

Instantly share code, notes, and snippets.

@artlbv
Last active July 28, 2023 04:46
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save artlbv/ec1153feb8644c613b4c7110b25e49ed to your computer and use it in GitHub Desktop.
Save artlbv/ec1153feb8644c613b4c7110b25e49ed to your computer and use it in GitHub Desktop.
Batch replace text in PDF
#!/bin/bash
#!-----------------------------------
#! Replace "oldtext" with "newtext"
#! inside all pdf files in the directory.
#! The old files are saved as *.pdf.bak
#!
#! Needs the pdftk package
#! http://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/
#!-------------------------------------
#
export LC_CTYPE=C
export LANG=C
oldtext=Preliminary
newtext="Private Work"
indir=${1-"."}
echo "Replacing $oldtext with $newtext in directory $indir"
for pdffile in `find $indir -type f -name "*.pdf"`;
do
echo "Processing $pdffile..."
cp $pdffile $pdffile.bak
pdftk $pdffile output $pdffile.tmp uncompress
sed -i -e "s/$oldtext/$newtext/g" $pdffile.tmp
if [ -e $pdffile ]
then
rm $pdffile
pdftk $pdffile.tmp output $pdffile compress
rm $pdffile.tmp
rm $pdffile.tmp-e
fi
done
@kupietools
Copy link

It fails if any PDFs have spaces in their names.

@kupietools
Copy link

Correction: On OS X, it doesn't work at all, even if PDFs don't have spaces in their names. It doesn't do any text replacement.

@Dids
Copy link

Dids commented Oct 4, 2018

Use this specific version on macOS and it won't hang and should work:
https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/pdftk_server-2.02-mac_osx-10.11-setup.pkg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment