Skip to content

Instantly share code, notes, and snippets.

@N0taN3rd
Created April 27, 2018 08:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save N0taN3rd/a5617aa72aa6e9bff61d111262f1d685 to your computer and use it in GitHub Desktop.
Save N0taN3rd/a5617aa72aa6e9bff61d111262f1d685 to your computer and use it in GitHub Desktop.
Appease the margin police at all costs
#!/usr/bin/env bash
####
# Original version of this file was created by Dr. Chuck Cartledge
# as a make file and can be found in his WS-DL blog post:
# https://ws-dl.blogspot.com/2014/07/2014-0-7-2-ode-to-margin-police-or-how.html
####
#### Set these variables to match your setup
CHECK="msThesis"
CHECKEXT="pdf"
MARGINDIR="Margins"
LATEX="pdflatex"
#### Values used to determine if a page violates margin police rules
goodSum=484704
threshold=10
#### Vars and functions used interally
checkWhat="${CHECK}.${CHECKEXT}"
resultsFile="marginResults.txt"
workDir=$(pwd)
#### The Magic
function checkMargins () {
cp ${checkWhat} ${MARGINDIR}
cd ${MARGINDIR}
pdftk ${checkWhat} burst
convert -size 35x17 xc:skyblue insertPage.png
convert -size 432x648 xc:skyblue insertBody.png
toProcess=$(ls pg_*.pdf)
echo "Processing ${#toProcess} pages"
for pgpdf in ${toProcess}; do
composite -geometry +108+72 insertBody.png ${pgpdf} temp.png
composite -geometry +508+36 insertPage.png temp.png tempAlso.png
convert tempAlso.png -flatten ${pgpdf/.pdf/.redacted.pdf}
echo "Processed ${pgpdf}"
done
pdftk *redacted.pdf cat output ${checkWhat/.pdf/.redacted.pdf}
toProcess=$(ls pg_*.redacted.pdf)
echo "Checking margins for ${#toProcess} pages"
for redacted in ${toProcess}; do
echo "Checking ${redacted} for margins violation"
convert ${redacted} -format %c histogram:info: > hist.txt
skyblue=$(grep SkyBlue hist.txt | cut -d: -f1 | tr -d "\n")
white=$(grep white hist.txt | cut -d: -f1 | tr -d "\n")
dif=$(expr ${goodSum} - ${skyblue} - ${white})
if [[ ${dif} -ge ${threshold} ]]; then
echo "${redacted} is bad"
echo "${redacted} is bad" >> ${resultsFile}
cat hist.txt >> ${resultsFile}
else
rm ${redacted} ${redacted/.redacted.pdf/.pdf}
fi
done
rm hist.txt doc_data.txt insertBody.png insertPage.png temp.png tempAlso.png
if [[ -f ${resultsFile} ]]; then
echo "Looks like there is still some work to be done in order to appease the margin police"
echo "Offending pages and results per page are found in the directory ${MARGINDIR}"
echo "Best of luck. Goodby and have nice day"
else
echo "No offending pages were found"
echo "Goodby and have nice day"
fi
}
if [[ ! -d ${MARGINDIR} ]]; then
mkdir ${MARGINDIR}
else
hasStuff=$(ls ${MARGINDIR})
if [[ ${#hasStuff} -gt 0 ]]; then
rm ${MARGINDIR}/*
fi
fi
if [[ ! -f ${checkWhat} ]]; then
echo "The pdf to be checked, ${checkWhat}, does not exist"
echo "Goodby and have nice day"
exit 1
fi
hasPDFTK=$(hash pdftk 2>/dev/null)
if [[ ! hasPDFTK ]]; then
echo "The program pdftk is not installed"
echo "Please install it via [sudo] apt install pdftk"
echo "Goodby and have nice day"
exit 1
fi
hasImageMagick=$(hash convert 2>/dev/null)
if [[ ! hasImageMagick ]]; then
echo "The program ImageMagick is not installed"
echo "Please install it via [sudo] apt install imagemagick"
echo "Goodby and have nice day"
exit 1
fi
checkMargins
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment