Created
October 8, 2018 16:32
-
-
Save mrkgnao/6898842bb0e0672f4d8898014c67de27 to your computer and use it in GitHub Desktop.
Crop pdfs with GhostScript
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
#!/usr/bin/env bash | |
set -euo pipefail | |
origdir=$(pwd) | |
tmpdir=$(mktemp -d) | |
echo "tmpdir: $tmpdir" | |
read -r width height < <(pdfinfo $1 | grep "Page size:" | sed 's/[a-zA-Z: ]*\([0-9]\+\)[x ]*\([0-9]\+\).*/\1 \2/') | |
npages=$(pdfinfo $1 | grep "Pages:" | sed 's/Pages: \+\([0-9]\+\).*/\1/') | |
echo "page count: $npages" | |
echo "(width, height) = ($width, $height)" | |
cp $1 $tmpdir/orig.pdf | |
cd $tmpdir | |
margleft=$2 | |
margtop=$3 | |
margright=$4 | |
margbot=$5 | |
left=$margleft | |
top=$((height-margtop)) | |
right=$((width-margright)) | |
bot=$margbot | |
echo -e "bbox:\n left: $left\n right: $right\n bot: $bot\n top: $top" | |
# Remove any cropbox already present | |
sed -i 's/CropBox/cROPbOX/g' orig.pdf | |
getTime() { | |
date +%s.%N | sed 's/[^0-9]//' | |
} | |
startTime=$(getTime) | |
elapsed() { | |
local timeNow=$(getTime) | |
echo $((timeNow-startTime)) | |
} | |
case "$6" in | |
end) | |
pageExtents='' | |
;; | |
*) | |
pageExtents="-dLastPage=$6" | |
;; | |
esac | |
# Crop | |
echo "cropping" | |
ls | |
gs $pageExtents -o cropped.pdf -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -c "[/CropBox [$left $bot $right $top]" -c " /PAGES pdfmark" -f orig.pdf \ | |
| grep --line-buffered '^Page' \ | |
| sed --unbuffered 's/[^0-9]//g' \ | |
| sed --unbuffered "s/.*/echo \"& (\$(( ( \$(date +%s.%N | sed 's:[^0-9]::') - $startTime )\/1000000000 ))s elapsed, estimated \$(( ( (\$(date +%s.%N | sed 's:[^0-9]::') - $startTime)\/100000000) * ($npages - &) \/ (& * 10) ))s remaining)\"/ep" \ | |
| stdbuf -o0 tr '\n' '\r' | |
echo "" | |
# | sed --unbuffered "s/Page \([0-9]\+\)/converted page \1 of $npages/" \ | |
mv cropped.pdf $origdir/crop-$1 | |
ls -l $origdir/crop-$1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment