Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save anselmobattisti/452f43151bde12278b40c0e655d18f93 to your computer and use it in GitHub Desktop.

Select an option

Save anselmobattisti/452f43151bde12278b40c0e655d18f93 to your computer and use it in GitHub Desktop.
extract_pages_from_pdf_and_remove_margim.sh
#!/bin/bash
# Check if input file is provided
if [ -z "$1" ]; then
echo "Usage: $0 file.pdf [output_directory]"
exit 1
fi
PDF_FILE="$1"
BASENAME=$(basename "$PDF_FILE" .pdf)
OUTPUT_DIR="${2:-${BASENAME}_pages}"
# Create output directory
mkdir -p "$OUTPUT_DIR"
# Convert PDF pages to PNG
pdftoppm "$PDF_FILE" "$OUTPUT_DIR/${BASENAME}" -png
# Crop each PNG to remove white margins using ImageMagick
for img in "$OUTPUT_DIR"/*.png; do
echo "Cropping $img..."
convert "$img" -trim +repage "$img"
done
echo "Conversion and cropping complete. Images saved in: $OUTPUT_DIR/"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment