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.

Revisions

  1. isurfer21 revised this gist Nov 19, 2024. No changes.
  2. isurfer21 created this gist Nov 19, 2024.
    25 changes: 25 additions & 0 deletions merge-pdfs.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    #!/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