Skip to content

Instantly share code, notes, and snippets.

@BlackHC
Created March 10, 2024 19:59
Show Gist options
  • Save BlackHC/56d3853a46badadd1afef75946f4751d to your computer and use it in GitHub Desktop.
Save BlackHC/56d3853a46badadd1afef75946f4751d to your computer and use it in GitHub Desktop.
#!/bin/bash
pdffile="$1"
prefix=$(basename "${pdffile}" .pdf)
convert -density 300 "${pdffile}" "${prefix}-%03d.png"
mogrify -background white -flatten "${prefix}-*.png"
total_pages=$(ls ${prefix}-*.png | wc -l)
for ((i=0; i<$total_pages; i+=2)); do
# Format page numbers
page1=$(printf "${prefix}-%03d.png" $i)
page2=$(printf "${prefix}-%03d.png" $(($i + 1)))
# Check if the second page exists
if [ -f "$page2" ]; then
output=$(printf "${prefix}-x2-%03d.png" $(($i / 2)))
convert "$page1" "$page2" +append "$output"
else
# If there's no pair for the last page, just copy it as is
cp "$page1" $(printf "${prefix}-x2-%03d.png" $(($i / 2)))
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment