Skip to content

Instantly share code, notes, and snippets.

@delucis
Last active February 13, 2020 11:40
Show Gist options
  • Save delucis/ca8b1ac260e00cd869ddecea996f9cc6 to your computer and use it in GitHub Desktop.
Save delucis/ca8b1ac260e00cd869ddecea996f9cc6 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Check all required CLIs are available
dependencies=( pdftoppm convert )
for dependency in "${dependencies[@]}"; do
command -v $dependency >/dev/null 2>&1 || { echo >&2 "‘$dependency’ command is required but it’s not installed. Aborting."; exit 1; }
done
# Create a directory to hold extracted images
if [ ! -d "images" ]; then
mkdir images
else
rm -R images/*
fi
# Make sure there’s an argument provided
if [ -z "$1" ]; then
echo "Error: no filename supplied."
exit 1
fi
SOURCEPDF=$1
FILENAME=$(echo "$SOURCEPDF" | cut -f 1 -d '.')
echo "Converting PDF pages to images..."
pdftoppm -png "$SOURCEPDF" images/out
echo "Converting images to PDF..."
cd images || exit 1
convert $( ls ) "../$FILENAME -PROOF IMAGE - DO NOT PRINT.pdf"
cd ../ || exit 1
# Tidy up, removing generated images
rm -R images
@delucis
Copy link
Author

delucis commented Feb 13, 2020

Homebrew-installable version available here: https://github.com/delucis/pdf2imgpdf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment