Created
July 30, 2025 13:40
-
-
Save anselmobattisti/452f43151bde12278b40c0e655d18f93 to your computer and use it in GitHub Desktop.
extract_pages_from_pdf_and_remove_margim.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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