Skip to content

Instantly share code, notes, and snippets.

@vielhuber
Last active November 28, 2024 12:26
Show Gist options
  • Save vielhuber/0fbf4f63aa24ae2b0de27237990f4c7d to your computer and use it in GitHub Desktop.
Save vielhuber/0fbf4f63aa24ae2b0de27237990f4c7d to your computer and use it in GitHub Desktop.
crop resize scale pdf #tools #linux

crop

#!/usr/bin/env bash

# set this to point values (1pt = 25.4 mm)
cropLeft=0
cropRight=298
cropTop=421
cropBottom=0

# calculate ghostscript cropbox values
currentWidth=$(pdfinfo -box input.pdf | grep "Page size:" | awk '{print $3}')
currentHeight=$(pdfinfo -box input.pdf | grep "Page size:" | awk '{print $5}')
left=$cropLeft
bottom=$cropBottom
right=$(echo "$currentWidth - $cropRight" | bc)
top=$(echo "$currentHeight - $cropTop" | bc)

# do the cropping
gs \
    -o output.pdf \
    -sDEVICE=pdfwrite \
    -c "[/CropBox [$left $bottom $right $top]" \
    -c " /PAGES pdfmark" \
    -f input.pdf

resize

  • if you want to "remove" the cropbox (e.g. resize the cropped pdf back to a4), do the following
# resize to a4
gs \
    -q \
    -dNOPAUSE \
    -dBATCH \
    -sDEVICE=pdfwrite \
    -dDEVICEWIDTHPOINTS=595 \
    -dDEVICEHEIGHTPOINTS=842 \
    -dFIXEDMEDIA \
    -dPDFFitPage \
    -sOutputFile="output-tmp.pdf" \
    -c "<</EndPage {0 eq {[/CropBox [0 0 595 842] /PAGE pdfmark true}{false}ifelse}>> setpagedevice" \
    -f "output.pdf"

# scale up by factor 2.00
gs \
    -q \
    -dNOPAUSE \
    -dBATCH \
    -sDEVICE=pdfwrite \
    -dDEVICEWIDTHPOINTS=595 \
    -dDEVICEHEIGHTPOINTS=842 \
    -sOutputFile="output-scaled.pdf" \
    -c "<</BeginPage{2.00 2.00 scale 0 0 translate}>> setpagedevice" \
    -f "output-tmp.pdf"

# remove tmp files
rm output-tmp.pdf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment