Skip to content

Instantly share code, notes, and snippets.

@ptheywood
Created July 30, 2019 14:31
Show Gist options
  • Save ptheywood/22eb6fd75c2ea79d9f35ba5dd7c3cdce to your computer and use it in GitHub Desktop.
Save ptheywood/22eb6fd75c2ea79d9f35ba5dd7c3cdce to your computer and use it in GitHub Desktop.
Combine 2 PDFS of (potentially) different scales
#! /bin/bash
A="a.pdf"
B="b.pdf"
DATE=$(date '+%Y-%m-%d')
OUTFILE="combined-$DATE.pdf"
# Get the page size from each pdf A and B
A_DIMS=$(pdfinfo $A | grep "Page size" | tr -s ' ' | cut -d" " -f"3,5")
B_DIMS=$(pdfinfo $B | grep "Page size" | tr -s ' ' | cut -d" " -f"3,5")
# Get the integer part of each height and width.
A_DIM_WIDTH=$(echo $A_DIMS | cut -d" " -f1)
A_DIM_HEIGHT=$(echo $A_DIMS | cut -d" " -f2)
B_DIM_WIDTH=$(echo $B_DIMS | cut -d" " -f1)
B_DIM_HEIGHT=$(echo $B_DIMS | cut -d" " -f2)
# Get the minimum width
TARGET_WIDTH=$A_DIM_WIDTH
if (( $(echo "$A_DIM_WIDTH > $B_DIM_WIDTH" |bc -l) )); then
TARGET_WIDTH=$B_DIM_WIDTH
fi
# Calculate the appropriate height based on the aspect ratio of each slide.
TARGET_HEIGHT_A=$(echo "$TARGET_WIDTH*($A_DIM_HEIGHT/$A_DIM_WIDTH)" | bc -l)
TARGET_HEIGHT_B=$(echo "$TARGET_WIDTH*($B_DIM_HEIGHT/$B_DIM_WIDTH)" | bc -l)
# Scale each pdf to temp
SCALED_A="scaled_$A"
SCALED_B="scaled_$B"
# -sPAPERSIZE=a4
gs -o "$SCALED_A" -sDEVICE=pdfwrite -dDEVICEWIDTHPOINTS=$TARGET_WIDTH -dDEVICEHEIGHTPOINTS=$TARGET_HEIGHT_A -dFIXEDMEDIA -dPDFFitPage -dCompatibilityLevel=1.4 "$A"
gs -o "$SCALED_B" -sDEVICE=pdfwrite -dDEVICEWIDTHPOINTS=$TARGET_WIDTH -dDEVICEHEIGHTPOINTS=$TARGET_HEIGHT_B -dFIXEDMEDIA -dPDFFitPage -dCompatibilityLevel=1.4 "$B"
# Cat the files.
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -dPDFSETTINGS=/printer -sOutputFile="$OUTFILE" "$SCALED_A" "$SCALED_B"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment