Skip to content

Instantly share code, notes, and snippets.

@isurfer21
Created November 19, 2024 13:05
Show Gist options
  • Save isurfer21/0d6c42b31276b2cb00579ebf9bbc86c8 to your computer and use it in GitHub Desktop.
Save isurfer21/0d6c42b31276b2cb00579ebf9bbc86c8 to your computer and use it in GitHub Desktop.
Here is a Bash script that takes command line arguments for the source PDF files and the output merged PDF file. The script uses Ghostscript (gs) to merge the specified PDF files.
#!/bin/bash
# Check if at least two arguments are provided (at least one source and one output)
if [ "$#" -lt 2 ]; then
echo "Usage: ${0##*/} output.pdf source1.pdf [source2.pdf ... sourceN.pdf]"
exit 1
fi
# The first argument is the output file
output_file="$1"
# The remaining arguments are the source PDF files
shift # Remove the first argument from the list
source_files="$@"
# Run Ghostscript to merge the PDFs
gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile="$output_file" $source_files
# Check if the merge was successful
if [ $? -eq 0 ]; then
echo "Merged PDF successfully!"
else
echo "Error while merging PDFs!"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment