Created
July 24, 2024 08:54
-
-
Save sjvrensburg/85ccbed27ef36b968017b59cd74af7bc to your computer and use it in GitHub Desktop.
Script to process SSRN articles for enlarged printing or viewing on e-reader
This file contains 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 | |
# Function to parse arguments | |
parse_arguments() { | |
while getopts ":m:" opt; do | |
case $opt in | |
m) | |
margin="$OPTARG" | |
;; | |
\?) | |
echo "Invalid option: -$OPTARG" | |
exit 1 | |
;; | |
:) | |
echo "Option -$OPTARG requires an argument." | |
exit 1 | |
;; | |
esac | |
done | |
shift $((OPTIND - 1)) | |
# Default margin | |
if [ -z "$margin" ]; then | |
margin=15 | |
fi | |
input_file="$1" | |
if [ -z "$input_file" ]; then | |
echo "Input file is required." | |
exit 1 | |
fi | |
} | |
# Parse arguments | |
parse_arguments "$@" | |
# Temporary file names | |
TEMP1=TEMPORARY01.pdf | |
TEMP2=TEMPORARY02.pdf | |
TEMP3=TEMPORARY03.pdf | |
TEMP4=TEMPORARY04.pdf | |
# Function to handle errors | |
handle_error() { | |
echo "An error occurred: $1" | |
rm -f "$TEMP1" "$TEMP2" "$TEMP3" "$TEMP4" | |
exit 1 | |
} | |
# Uncompress PDF | |
qpdf --stream-data=uncompress "$input_file" "$TEMP1" || handle_error "qpdf failed" | |
# Remove "Electronic copy available at:" text | |
sed 's/Electronic copy available at: .*//' < "$TEMP1" > "$TEMP2" || handle_error "sed failed" | |
# Optimize PDF | |
mutool clean "$TEMP2" "$TEMP3" || handle_error "mutool clean failed" | |
# Compress PDF | |
qpdf --compress-streams=y --object-streams=generate "$TEMP3" "$TEMP4" || handle_error "qpdf failed" | |
# Crop PDF | |
pdfcrop --margins "$margin" "$TEMP4" "${input_file%.*}_processed.${input_file##*.}" || handle_error "pdfcrop failed" | |
# Remove temporary files | |
rm -f "$TEMP1" "$TEMP2" "$TEMP3" "$TEMP4" | |
echo "Processing completed successfully. Output file: ${input_file%.*}_processed.${input_file##*.}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment