Last active
July 28, 2023 04:46
-
-
Save artlbv/ec1153feb8644c613b4c7110b25e49ed to your computer and use it in GitHub Desktop.
Batch replace text in 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 | |
#!----------------------------------- | |
#! 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 |
It fails if any PDFs have spaces in their names.
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.
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
Based on https://github.com/cmstas/CMS2_ArchiveJul2013/blob/master/NtupleMacros/Tools/replacepdftext.sh