38 lines
1.1 KiB
Bash
38 lines
1.1 KiB
Bash
#!/bin/bash
|
|
|
|
# Check if pdftk and ghostscript are installed
|
|
if ! command -v pdftk &>/dev/null || ! command -v gs &>/dev/null; then
|
|
echo "Error: pdftk and ghostscript are required but not installed."
|
|
exit 1
|
|
fi
|
|
|
|
# Check for input arguments
|
|
if [ "$#" -ne 3 ]; then
|
|
echo "Replaces the last page of a pdf file (input) with another (insert) and saves the file (output)."
|
|
echo "Usage: $0 input.pdf insert.pdf output.pdf"
|
|
exit 1
|
|
fi
|
|
|
|
input_pdf="$1"
|
|
insert_pdf="$2"
|
|
output_pdf="$3"
|
|
|
|
# Get the number of pages in the input PDF
|
|
num_pages=$(pdftk "$input_pdf" dump_data | grep NumberOfPages | awk '{print $2}')
|
|
|
|
if [ -z "$num_pages" ]; then
|
|
echo "Error: Unable to determine the number of pages in the input PDF."
|
|
exit 1
|
|
fi
|
|
|
|
# Extract all pages except the last one
|
|
pdftk "$input_pdf" cat 1-$(($num_pages - 1)) output temp_removed_last.pdf
|
|
|
|
# Merge the modified PDF with the insert PDF
|
|
pdftk temp_removed_last.pdf "$insert_pdf" cat output "$output_pdf"
|
|
|
|
# Clean up temporary files
|
|
rm -f temp_removed_last.pdf
|
|
|
|
echo "Successfully created $output_pdf by replacing the last page with $insert_pdf."
|